Quick Visual C++ question

Status
Not open for further replies.

Chrisblue

Ars Legatus Legionis
12,154
OK, I made a .cpp file (no header) that contains an ADT and I also made another .cpp file that contains a driver program for the ADT. Both files compile fine separately with no warnings nor errors. However, when I try to Debug build the project I get this:

--------------------Configuration: QueType - Win32 Debug--------------------
Compiling...
QueType.cpp
QueTypeDriver.cpp
Linking...
QueTypeDriver.obj : error LNK2005: "public: __thiscall QueType::QueType(void)" (??0QueType@@QAE@XZ) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: void __thiscall QueType::MakeEmpty(void)" (?MakeEmpty@QueType@@QAEXXZ) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: bool __thiscall QueType::IsEmpty(void)const " (?IsEmpty@QueType@@QBE_NXZ) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: bool __thiscall QueType::IsFull(void)const " (?IsFull@QueType@@QBE_NXZ) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: int __thiscall QueType::Items(void)const " (?Items@QueType@@QBEHXZ) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: void __thiscall QueType::Enqueue(int)" (?Enqueue@QueType@@QAEXH@Z) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: void __thiscall QueType::Dequeue(int &)" (?Dequeue@QueType@@QAEXAAH@Z) already defined in QueType.obj
QueTypeDriver.obj : error LNK2005: "public: void __thiscall QueType::printQueue(class ostream &)" (?PrintQueue@QueType@@QAEXAAVostream@@@Z) already defined in QueType.obj
Debug/QueType.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

QueType.exe - 9 error(s), 0 warning(s)

And I have NO idea what that is. I think it is just something simple I forgot but I am rather inexperienced with Visual C++ (C++ in general) and a bit pressed for time. Please help!
 
You error stems from the fact that you put your ADT in a cpp file, and then (presumably) included that cpp in you driver file, which is also a cpp file. The compiler then compiles your ADT twice and ends up the multiply definded symbols.

ADTs, or really any template (function or class) needs to be in a header file. You should never ever use an include directive that includes a cpp file. You should only include header files.

Also you need to wrap the code contained in your header file in ifndef/endif just as Jackass JoeJoe already said. The purpose of this is to prevent the classes and data in your header file from being compiled twice. Since compiling twice will redefine everything in your header file, resulting in mulitply defined symbols.
 
Status
Not open for further replies.