 |
 |
C++, Xcode telling me main() needs int?!
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2004
Location: In front of computer
Status:
Offline
|
|
Hi Folks,
I have just begun to code C++ for the first time starting yesterday and I can't even get the very first most basic script completed that I found in a tutorial. Now, the tutorial is done with the MS compiler, but when I write the exact same code in xCode I get this error:
error: 'main' must return 'int'
The code is as follows:
#include<iostream.h>
void main()
{
//define variables
float num1;
float num2;
float total;
//Enter data for the variables
cout << "enter a value for the first variable: ";
cin >> num1;
cout << "enter a value for the second variable: ";
cin >> num2;
//add the two numbers together
total = num1 + num2;
//display the results
cout << "the sum of the Numbers = " << total << endl;
}
now, I read in a bunch of places that different compilers require different returns for main? Frankly, I have no idea of what "main" even does. I thought that in C++ void main(){ } declared a function called "main" but it turns out there is alread some type of function built called main that it references. Very confused.
Anyway, is there ANY WAY to get this to work? Is it Xcode, main, C++ or just silly ol' me?
Thanks folks.
-i
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Most C compilers look for main to be declared like:
int main(int argc, char* argv[])
Then add:
return 0;
at the end of main method.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Originally posted by iPhotoStuff:
Hi Folks,
now, I read in a bunch of places that different compilers require different returns for main? Frankly, I have no idea of what "main" even does. I thought that in C++ void main(){ } declared a function called "main" but it turns out there is alread some type of function built called main that it references. Very confused.
Anyway, is there ANY WAY to get this to work? Is it Xcode, main, C++ or just silly ol' me?
Thanks folks.
-i
The main method is required because the C/C++ compiler needs to know where to start executing code. You can have tons of files (classes, header files, etc.) but a "program" will only have one main method.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 2001
Status:
Offline
|
|
the return value usually just tells the OS the exit status of the program. Just change that main function to read: int main() { ... } and at the end of the program before the last bracket put something like 'return 0;'
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
another spot that discusses something similar to this question is down a little ways titled "help please" posted by zanyterp . . . (me).
perhaps can help little bit more. . . .while on this topic here, if i declare a function that main() calls, does void functionName() work? (including the ';' as needed of course.)
hope that makes some sense . . .
thanks!
|
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by zanyterp:
while on this topic here, if i declare a function that main() calls, does void functionName() work? (including the ';' as needed of course.)
void functionName(); is a valid declaration. You should just be including the header file, though.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Originally posted by zanyterp:
another spot that discusses something similar to this question is down a little ways titled "help please" posted by zanyterp . . . (me).
perhaps can help little bit more. . . .while on this topic here, if i declare a function that main() calls, does void functionName() work? (including the ';' as needed of course.)
hope that makes some sense . . .
thanks!
Yes, you can create void functions. It's just that some compilers expect the main method to be declared as int main...
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by PBG4 User:
Yes, you can create void functions. It's just that some compilers expect the main method to be declared as int main...
The C++ standard requires int main() or int main(int, char**).
(Last edited by Chuckit; Dec 30, 2004 at 01:57 PM.
)
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by Chuckit:
void functionName(); is a valid declaration. You should just be including the header file, though.
so instead of declaring the function in the same file as main() create a separate file that contains it and include it as #include"fileName".cpp? or does it depend on the file?
thanks!
|
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Normally you declare your functions in a header file and define them in an implementation file. Like this:
Code:
// SomeClass.h
#ifndef SOMECLASS_H
#define SOMECLASS_H
class SomeClass {
int variable;
public:
bool doSomethingWithVariable(int arg);
}
#endif // SOMECLASS_H
Code:
// SomeClass.cpp
#include "SomeClass.h"
bool SomeClass::doSomethingWithVariable(int arg) {
// Do something...
}
Code:
// main.cpp
#include "SomeClass.h"
int main() {
SomeClass sc;
sc.doSomethingWithVariable(12);
}
If you just have one other function, you could just stick it above main in main.cpp, but that isn't very modular design.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by Chuckit:
Normally you declare your functions in a header file and define them in an implementation file. Like this:
Code:
// SomeClass.h
#ifndef SOMECLASS_H
#define SOMECLASS_H
class SomeClass {
int variable;
public:
bool doSomethingWithVariable(int arg);
}
#endif // SOMECLASS_H
Code:
// SomeClass.cpp
#include "SomeClass.h"
bool SomeClass::doSomethingWithVariable(int arg) {
// Do something...
}
Code:
// main.cpp
#include "SomeClass.h"
int main() {
SomeClass sc;
sc.doSomethingWithVariable(12);
}
If you just have one other function, you could just stick it above main in main.cpp, but that isn't very modular design.
so including SomeClass.cpp in the main program is not modular or bad programming style? or could it just be that i am not sure how to do the SomeClass.h with the #define, #ifndef code because we haven't done that in class yet. is there a difference between making SomeClass.h and SomeClass.cpp and including in the program main()?
thanks! (and sorry for all the questions; just trying to learn and make sure i have nice, modular, editable, and readable code ;-))
|
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Well, you don't want to include a file that's going to be compiled in another file that's going to be compiled. That will just confuse things. Also, header files do make your design more modular by separating the interface (e.g. what arguments a function takes) from the implementation details. This means that you can change the implementation of a class and it won't affect anything else — nothing else will even need a recompile. What the world sees can stay the same.
The #ifdef hack is just to make up for the fact that C++ doesn't have a proper import feature. It keeps a header from actually being included more than once. (If the header has already been included, the compiler will just see an empty file.)
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Originally posted by Chuckit:
Normally you declare your functions in a header file and define them in an implementation file. Like this:
Code:
// SomeClass.h
#ifndef SOMECLASS_H
#define SOMECLASS_H
class SomeClass {
int variable;
public:
bool doSomethingWithVariable(int arg);
}
#endif // SOMECLASS_H
Code:
// SomeClass.cpp
#include "SomeClass.h"
bool SomeClass::doSomethingWithVariable(int arg) {
// Do something...
}
Code:
// main.cpp
#include "SomeClass.h"
int main() {
SomeClass sc;
sc.doSomethingWithVariable(12);
}
If you just have one other function, you could just stick it above main in main.cpp, but that isn't very modular design.
Since doSomethingWithVariable(int arg) returns a bool value, the main method should look more like: (my changes in bold)
Code:
// main.cpp
#include "SomeClass.h"
int main() {
SomeClass sc;
bool result;
result = sc.doSomethingWithVariable(12);
}
This way you catch the returned bool value generated by the called function.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status:
Offline
|
|
Originally posted by Chuckit:
The C++ standard requires int main() or int main(int, char**).
Thanks for the clarification, Chuckit.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|