Archived

This topic is now archived and is closed to further replies.

StoNeD_TrOLL

wtf???

Recommended Posts

StoNeD_TrOLL    122
Im using CodeWarrior Learning Edditon.My project has two files that have no flaws(made sure).When i got to run my project i get a link error about something wrong with main in both files.Whats wrong here?

Share this post


Link to post
Share on other sites
jenova    122
please list the exact error message, and you''ll probably get a more appropriate response.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
samosa    122
did u check to make suere that u have the proper header fies included? u sould also make suer u have the proper .lib files included

,Matt

-= kill one, your a murderer, kill thousands your a conquerer =-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
#include <iostream> is a header file right?
and i dont know what .lib files are.
here are the errors:
Link Error : multiply-defined ''main'' (descriptor)
Defined in bkla.cpp
Defined in HelloWorld.cp

Link Error : multiply-defined ''main'' (code)
Defined in bkla.cpp
Defined in HelloWorld.cp

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Have you got more than one WinMain or main() function? If so, that''s your problem right there.

Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
quote:
Original post by Anonymous Poster
Have you got more than one WinMain or main() function? If so, that''s your problem right there.

wait,i only need one main out of my two files?

Share this post


Link to post
Share on other sites
Drizzt DoUrden    100
Only one main, always remember that.

By the way, anything that is a .h is a header file. Header files basically are used to include the .lib in your program, but you have to add the .lib to your project. If your not using an API like Windows or DX/OpenGL, you don't have to worry about .libs yet.

You only need one main in your whole project, because you are including the other files which are connected to the file that has main in it.
------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net's Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)


Edited by - Drizzt DoUrden on January 12, 2002 12:39:20 PM

Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
quote:
Original post by Drizzt DoUrden
Only one main, always remember that.
------------------------------------------------------
I know one main per a file, but do you mean one main out of all my files in my project??


Share this post


Link to post
Share on other sites
jenova    122
definitely one "main" per project. the linker looks for a function named "main" and sets the program to start here. thus if there were two "main"''s then it wouldn''t know which one to choose.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
Thank you!!!So in my other files i just put in the header and go straight to the coding?like this:
#include <iostream>
{
std::cout <<"Hi!\n";
return 0;
}


Edited by - StoNeD_TrOLL on January 12, 2002 12:53:31 PM

Share this post


Link to post
Share on other sites
Drizzt DoUrden    100
No. You make another function, like -

  
int PrintHi()
{
std::cout <<"Hi!\n";
return 0;
}


Remember to use function prototypes. Have you read any books/tutorials on C++ yet?

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net''s Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)

Share this post


Link to post
Share on other sites
Martee    476
No, you create functions with names other than main.

int myReallyCoolUsefulFunction(void)
{
std::cout << "Hi!\n";
return 0;
}

Edit: Crap! You beat me to it...

Edited by - Martee on January 12, 2002 1:02:27 PM

Share this post


Link to post
Share on other sites
Drizzt DoUrden    100
Sorry.

Stoned Troll, you should check out the book ''sams teach yourself c++ in 21 days.'' It''s a really great book; you''ll learn alot from it.

------------------------------
Simple DirectMedia Layer:

Main Site - (www.libsdl.org)
Cone3D Tutorials- (cone3D.gamedev.net)
GameDev.net''s Tutorials - (Here)

OpenGL:

Main Site - (www.opengl.org)
NeHe Tutorials - (nehe.gamedev.net)
Online Books - (Red Book) (Blue Book)

Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
Im reading a c++in twenty four hours.And Drizzt,when i do your code my complier gives me this:
Error : undefined identifier 'std'
bkla.cpp line 3 std:: cout <<"Hi!\n";



Edited by - StoNeD_TrOLL on January 12, 2002 1:07:55 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Just leave out the "std::". It''s not supposed to be there.

Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
The #include <iostream.h> worked but now where back to main!!!!How do you guys make a file when you already have a file with main???

Edited by - StoNeD_TrOLL on January 12, 2002 4:57:47 PM

Share this post


Link to post
Share on other sites
Oluseyi    2116
Hold it!

Erroneous info is being passed around here. <iostream.h> is the old-style C++ stream I/O header; the new one is <iostream>. You should aim to always use the new header unless you''re just prototyping some code you won''t have to live with in the future.

Having included the new-style header, cin, cout and all other iostream-defined objects and classes are declared under the std namespace. You can import the entire namespace into your working namespace (the top-level unnamed namespace), but that''s a bad idea in the long run.
using namespace std; 

Alternatively, you can import particular entities from a namespace:
using std::cout; 

After this line above, you can refer to cout anywhere in the same file (if it''s a header file, you can refer to cout without prefix in any other files that include it).

The third option is to explicitly qualify the object name every time you use it:
std::cout << "H1!" << std::endl; 

As you can see, endl is also under the std namespace.

Finally, there''s no substitute for a good book and tons of trial and error. If you have more problems, the great folks here at GameDev are only too willing to help.

Happy Hacking!

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You whole project needs to only have one main() function, as others have pointed out. The other .c or .cpp files are used to declare other functions to use with your program.

-------In file1.cpp--------------

#include <iostream>
using namespace std;

void MyOtherFunction(char *str);

int main(int argc, char *argv[])
{
MyOtherFunction("hello";
return 0;
}


-----In file2.cpp---------------

#include <iostream>
using namespace std;

void MyOtherFunction(char *str)
{
printf("%s", str);
return
}


Multiple source files are used only to organize source code and they only build to one program. In the above example you could use the file2.cpp in another project that needs the MyOtherFunction() function.

I hope that clears the mystery behind it.


Oh for the other guys that are confused about std::,

when you include <iostream> function/classes are in "namespaces" and you need to specify "using namespace std" to use classes in that namespace. When you include <iostream.h>, this doesn''t have any namespace and is similar to the older C style headers.

By saying "using namespace std" you tell the compiler that you want to use ALL the classes/functions in that namespace, but if you want to use only a few, you could do "using std::cout;" using std::cin;" and this will let you call cin and cout without typinh "std::cout << "hello"" all the time.

Ok, enough typing for now.


Share this post


Link to post
Share on other sites
StoNeD_TrOLL    122
Heres my little file2.cpp:
#include <iostream>
using namespace std;
void
{
std::cout <<"This dosnt work";
}
i get these errors:
Error : declaration syntax error
ngw.cpp line 4 {

Error : declaration syntax error
ngw.cpp line 5 std::cout <<"This dosnt work";

Error : declaration syntax error
ngw.cpp line 6 }

what is wrong now?!!!

Edited by - StoNeD_TrOLL on January 12, 2002 6:54:05 PM

Share this post


Link to post
Share on other sites
Oluseyi    2116
quote:
Original post by StoNeD_TrOLL
void
{

void is a return type declarator or an indicator that a function takes no parameters. You can not declare a variable of type void, and you can not simply place the void keyword in the middle of nowhere.

Try this:
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "This works." << endl;
return 0;
}

(The C++ standard requires main to have a return type of int).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites