Dev-cpp compiler error with class function definitions

I have a program that where I have main including a file call list.cpp which includes list.h. list.h includes all c++ headers and has using namespace std; in it. I get the following errors in dev-cpp.

C:\Dev-Cpp\prog3\list.o(.text+0x0) In function `ZSt4swapIPN6MyList4NodeEEvRT_S4_':
/Dev-Cpp/prog3/list.cpp C:\Dev-Cpp\prog3\C multiple definition of `MyList::MyList()'
/Dev-Cpp/include/c++/3.3.1/iomanip C:\Dev-Cpp\prog3\prog3.o(.text+0x0):C first defined here

It does this for every function of the class. I have the class multiple includes protection. I can get rid of these errors by putting all the function definitions in list.h, include list.h in main, and remove list.cpp from the project.

Any ideas?
You should never #include .cpp files, only header files.
You should never #include .cpp files, only header files.


Well that's how it's done with classes. There's a class declaration, .h, and implementation file, .cpp.

obligatory link:

http://cplusplus.com/forum/articles/10627/


This is the way I've done it and it works on every other program of mine but this one. Again g++ has no problem compiling it. Just dev-cpp.

This is where header files come in. Header files allow you to make the interface (in this case, the class MyClass) visible to other .cpp files, while keeping the implementation (in this case, MyClass's member function bodies) in its own .cpp file. That same example again, but tweaked slightly:

1
2
3
4
5
6
7
// in myclass.h
class MyClass
{
public:
  void foo();
  int bar;
};


1
2
3
4
5
6
// in myclass.cpp
#include "myclass.h"

void MyClass::foo()
{
}


1
2
3
4
5
6
7
8
//in main.cpp
#include "myclass.h"  // defines MyClass

int main()
{
  MyClass a; // no longer produces an error, because MyClass is defined
  return 0;
}



Well then what gives? It's the same way I've been doing it and dev-cpp all of a sudden doesn't like it?
Last edited on
Topic archived. No new replies allowed.