问题描述:

I'm having a problem compiling a multiple-part project. I thought I had included the correct #ifndef/#define statements and included header files correctly, but apparently not. When attempting to compile this error came up:

[23:18]andrew final_project$:g++ driver2.cpp fish.cpp bay.cpp

ld: duplicate symbol Fish::Fish() in /var/folders/cc/cc+as-5yHSqg0Jcr+X2+uk+++TI/-Tmp-//ccqNXrPE.o

and /var/folders/cc/cc+as-5yHSqg0Jcr+X2+uk+++TI/-Tmp-//cc5dOIm5.o for

architecture x86_64

collect2: ld returned 1 exit status

I can include more information if necessary, but my two header files (fish.h and bay.h) are bookended with.

#ifndef FOO

#define FOO

...

#endif

my two implementation files are fish.cpp and bay.cpp.

fish.cpp: #include "fish.h"

bay.cpp: #include "bay.h" #include "fish.h"

My driver program driver2.cpp: #include "fish.cpp" #include "bay.cpp"

I assume the problem lies in the fact that bay.cpp includes both fish.h and bay.h, but when I don't include fish.h the "bay" (a grid of fish) is undefined. I would guess the problem isn't too hard, but I can't seem to find a solution.

--Thanks in advance,

SegFaults McGee

网友答案:

Don't include the .cpp files in driver2.cpp. Include fish.h and bay.h instead.

相关阅读:
Top