error LNK2005 - already defined

Jan 29, 2014 at 11:37pm
hello,

I know this error has been published many times but I couldn't find a solution to mine.

I have 4 files:
def.h
mms.h
mms.cpp
runMms.cpp

In the first one I define a struct. In the second I declare/create a new class called mms and I include "def.h". The third one is the cpp file of the class and I include in it "mms.h" (no, I don't include in it "def.h"). The fourth and last has the 'main' and I include in it "mms.h"

For some reason I'm getting the "error: lnk2005 alread defined ..." I know it usually has to do with putting some header file or definitions twice but I can't see where I got wrong.

I would appreciate it if any helprs could attached a code sample or something like that.

Thanks.
Jan 29, 2014 at 11:46pm
ilanhak wrote:
The fourth [...] I include in it "mms.h"
The fourth one is a source .cpp file - you had better not be writing #include "anything.cpp" ever.
Jan 30, 2014 at 12:25am
As LB said, do not include other source files.

Another problem might be that you have a function body fully defined in a header (which will be a problem unless the function is a template or is inlined).

Or.. another problem might be that you have a variable declared in a header.

It's impossible to be more specific without actually seeing the error message in full. The error message will actually tell you exactly what symbol is being multiply defined... so it helps to read/post it.
Jan 30, 2014 at 12:38am
I know it usually has to do with putting some header file or definitions twice but I can't see where I got wrong.



I hope you use include guards in header files (or #pragma once as you are using Visual Studio)
Jan 30, 2014 at 9:34am
The problem was with inline declartions of struct data members in "def.h".

Thanks....
Jan 30, 2014 at 9:22pm
Are you using #ifndef, #define, & #endif?? If not, it could be redefining it.
Topic archived. No new replies allowed.