简体   繁体   中英

Simple compiler question

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM