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

Started by
6 comments, last by Sneftel 16 years ago
[ 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]
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
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.
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?
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++.
//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.
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();".
Moved to For Beginners.

This topic is closed to new replies.

Advertisement