Problems Linking C++ Files

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 9
Reputation: i_like_pi is an unknown quantity at this point 
Solved Threads: 0
i_like_pi i_like_pi is offline Offline
Newbie Poster

Problems Linking C++ Files

 
0
  #1
Mar 10th, 2007
Hi, I'm a newbie to C++. I use Windows XP Home Edition and VC++ .NET 2003.

I have just recently started creating large scale C++ projects with more than one file. I've been having troubles linking the files together.

I solved the problem by adding "/FORCE:MULTIPLE" to the command line, but the compiler still gives linker warnings. I don't feel satisfied just by cheating my way out.

The real source files are large, and I know that it is a linker error, so I created smaller files with a similar structure that give the same errors.

Here are the source files:

stdafx.cpp

#include "stdafx.h"

Untitled.cpp

#include "stdafx.h"
#include "MyClass.cpp"
#include "OtherClass.cpp"
 
usingnamespace std;
 
int main()
{
myClass Class;
otherClass Class2;
 
return 0;
}

MyClass.cpp

#include "stdafx.h"
 
usingnamespace std;
 
class myClass
{
public:
 
myClass();
 
int variable;
 
void function1();
void function2();
 
};
 
myClass::myClass()
{
variable = 0;
}
 
void myClass::function1()
{
variable = 1;
}
 
void myClass::function2()
{
variable = 2;
}

OtherClass.cpp

#include "stdafx.h"
 
class otherClass
{
public:
 
otherClass();
 
int variable;
 
void function1();
void function2();
 
};
 
otherClass::otherClass()
{
variable = 0;
}
 
void otherClass::function1()
{
variable = 1;
}
 
void otherClass::function2()
{
variable = 2;
}

And here are the errors:

MyClass.obj : error LNK2005: "public: __thiscall myClass::myClass(void)" (??0myClass@@QAE@XZ) already defined in Untitled.obj
MyClass.obj : error LNK2005: "public: void __thiscall myClass::function1(void)" (?function1@myClass@@QAEXXZ) already defined in Untitled.obj
MyClass.obj : error LNK2005: "public: void __thiscall myClass::function2(void)" (?function2@myClass@@QAEXXZ) already defined in Untitled.obj
OtherClass.obj : error LNK2005: "public: __thiscall otherClass:0therClass(void)" (??0otherClass@@QAE@XZ) already defined in Untitled.obj
OtherClass.obj : error LNK2005: "public: void __thiscall otherClass::function1(void)" (?function1@otherClass@@QAEXXZ) already defined in Untitled.obj
OtherClass.obj : error LNK2005: "public: void __thiscall otherClass::function2(void)" (?function2@otherClass@@QAEXXZ) already defined in Untitled.obj
Debug/Untitled.exe : fatal error LNK1169: one or more multiply defined symbols found

Thanks in advance! :cheesy:

btw: Do error messages need "code" tags?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problems Linking C++ Files

 
0
  #2
Mar 10th, 2007
A big THANK YOU for using code tags! You are one of the few newbies that actually read the rules and follow them from the first post.

2 things that caught my eye right away:

Never include cpp files. Since they are implementation files, all that should be required is that you add them to your project's workspace.

Secondly, you need to place you class definitions in header files (.h). These header files are then included by your .cpp files that use the functions, or in the case of the class method implementation cpp files, define the functions.

In case that wasn't clear, here's what I mean:
  1. // myclass.h
  2. #ifndef MYCLASS_H // note: these are very important!
  3. #define MYCLASS_H
  4. class myClass {
  5.  
  6. public:
  7. void myFunc();
  8. // blah blah blah
  9.  
  10. };
  11. #endif
  12.  
  13. // myclass.cpp
  14. #include "myclass.h"
  15.  
  16. void myFunc::myClass() {
  17. // etc
  18. }
  19.  
  20. // main.cpp
  21.  
  22. #include "myclass.h"
  23.  
  24. int main() {
  25.  
  26. myClass myObj;
  27.  
  28. }
Last edited by John A; Mar 10th, 2007 at 7:17 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 9
Reputation: i_like_pi is an unknown quantity at this point 
Solved Threads: 0
i_like_pi i_like_pi is offline Offline
Newbie Poster

Re: Problems Linking C++ Files

 
0
  #3
Mar 10th, 2007
Thanks! Your suggestion solved the errors! My only question is, why do i need the #ifndefine, #define, #endif etc. ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problems Linking C++ Files

 
0
  #4
Mar 10th, 2007
Originally Posted by i_like_pi View Post
Thanks! Your suggestion solved the errors! My only question is, why do i need the #ifndefine, #define, #endif etc. ?
It's to prevent multiple inclusions. Let's take that previous example that we had before, but now you need to include this class in other files as well:

  1. // otherclass.cpp
  2.  
  3. #include "myclass.h"
  4.  
  5. // blah blah blah
  6.  
  7. // anotherclass.cpp
  8.  
  9. #include "myclass.h"
  10.  
  11. // blah blah blah

Now, normally the header code would be pasted into the cpp files by the compiler's preprocessor. But this would cause errors because you are redefining the class. You are only allowed to define a class once. Using #ifndef/#define/#endif checks to see if it has already been included by another file. If so, it does nothing, and no errors happen.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 9
Reputation: i_like_pi is an unknown quantity at this point 
Solved Threads: 0
i_like_pi i_like_pi is offline Offline
Newbie Poster

Re: Problems Linking C++ Files

 
0
  #5
Mar 10th, 2007
I see, so the commands that start with "#" are messages to the compiler. Thanks alot!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problems Linking C++ Files

 
0
  #6
Mar 10th, 2007
>I see, so the commands that start with "#" are messages to the compiler.
Yup. The technical name for them is "preprocessor directives", so don't freak out if you hear that term.

>Thanks alot!
Glad I could help.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC