Jump to content
  • Advertisement
Sign in to follow this  
chamaera

"Multiple definition" error when using classes in Dev C++

This topic is 3882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[ Important note: The "solution" posted here is wrong. --Sneftel ] To alleviate the problem, go into the Project Options (Alt + P). In the files tab, find the name of the .cpp file that has your class in, click it. Click once in the box next to "include in linking." Vwa-la! Problem solved. [Edited by - Sneftel on April 1, 2008 3:24:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
You should be using inclusion guards for the header files where your classes (or other types) are declared. For example:


#ifndef FOO_H_
#define FOO_H_

class Foo
{

};

#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by chamaera
To alleviate the problem, go into the Project Options (Alt + P). In the files tab, find the name of the .cpp file that has your class in, click it. Click once in the box next to "include in linking." Vwa-la! Problem solved.
If that does what I think it does (And never having used DevCpp myself, I'm clearly an expert :P), that is the last thing you want to do. If you're not including a source file while linking, then you're just as well not including it in the project - Unless you're #including it, in which case you have a hell of a lot more problems.

Share this post


Link to post
Share on other sites
In regards to MJP: Dev C++ is funny about classes in projects. The inclusion guards don't do anything to help.. at least not from all my experiences with it.

And Evil Steve: Yes, not including it in the project is the same shpiel and it solves the problems. Either that or stick all the foo::foo() stuff into a header. But if it's in a .cpp, it goes all buggy. Dev's picky, I guess?

Share this post


Link to post
Share on other sites
Quote:
Original post by chamaera
In regards to MJP: Dev C++ is funny about classes in projects. The inclusion guards don't do anything to help.. at least not from all my experiences with it.


I suspect you may be doing something wrong in your code, can you provide an example of when you are seeing the multiple definition error that you've proposed this "fix" for?

Regardless, Dev-C++ is old an no longer being updated. Use a newer IDE like Visual Studio Express, Code::Blocks, or wxDev-C++.

Share this post


Link to post
Share on other sites
//IN MY HEADER (foo.h, let's call it)
#ifndef _FOO_H_
#define _FOO_H_

class foo
{
foo();
//other stuff
}

#include "foo.cpp"
#endif



//IN MY CLASS'S CPP (foo.cpp)
foo::foo()
{
//whatever
}



//IN MY MAIN
#include "foo.h"

int main()
{
//code
foo f1();
//another call to a function in the class foo

cin.get();
return 0;
}


The errors are multiple definition errors. The "multiple definition" and "previously defined here" lines are the same. It says it's redefined on that same line. Or, at seemingly random intervals, there are no line numbers at all.

And someone mentioned a problem with #including foo.cpp at the end of the header, but it works fine elsewhere... just not with classes.

Share this post


Link to post
Share on other sites
Problem 1: Never include source (.cpp) files. Doing so indicates that you do not yet understand the C++ compilation model. This may help.

Problem 2: _FOO_H_ is a special symbol, reserved for use by the compiler. Anything that starts with an underscore and an uppercase character, or starting with 2 consecutive underscores is such a symbol. Prefer FOO_H.

Problem 3: foo.cpp should include foo.h

Problem 3: The line "foo f1();" doesn't make a foo instance called f1 by calling the default constructor. If you look carefully, you will see that that is the same syntax used to declare a function (consider: int main()). You can declare functions inside another function (although you cannot define them there). To do what you want, use either "foo f1;", or if you want to be verbose "foo f1 = foo();".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!