Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++, Xcode telling me main() needs int?!

C++, Xcode telling me main() needs int?!
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2004
Location: In front of computer
Status: Offline
Reply With Quote
Dec 30, 2004, 08:50 AM
 
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
Reply With Quote
Dec 30, 2004, 11:57 AM
 
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.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Dec 30, 2004, 12:12 PM
 
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.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Dec 30, 2004, 12:44 PM
 
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
Reply With Quote
Dec 30, 2004, 01:21 PM
 
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
Reply With Quote
Dec 30, 2004, 01:40 PM
 
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
Reply With Quote
Dec 30, 2004, 01:40 PM
 
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...
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Dec 30, 2004, 01:44 PM
 
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
Reply With Quote
Dec 30, 2004, 05:22 PM
 
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
Reply With Quote
Dec 30, 2004, 06:05 PM
 
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
Reply With Quote
Dec 30, 2004, 06:45 PM
 
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
Reply With Quote
Dec 31, 2004, 04:32 AM
 
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
Reply With Quote
Dec 31, 2004, 06:45 AM
 
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.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Dec 31, 2004, 06:53 AM
 
Originally posted by Chuckit:
The C++ standard requires int main() or int main(int, char**).
Thanks for the clarification, Chuckit.
20" iMac G5! :D AND MacBook 1.83GHz!
Canon Digital Rebel Kit + 75 - 300mm lens. Yum Yum! :D
Check out my OS X Musical Scales program
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:03 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2