Results 1 to 5 of 5
Hello,
I am sure someone there can help me. I am trying to simply build a test program having a super class and sub class. Each class has a header ...
- 09-04-2009 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 2
What am I doing wrong--- "undefined reference"
Hello,
I am sure someone there can help me. I am trying to simply build a test program having a super class and sub class. Each class has a header and cpp file. So I have two classes, Super and Sub plus a driver program.
Here they are (from the book Professional C++).
//Super.h file
#include <iostream>
using namespace std;
class Super
{
public:
Super();
virtual void someMethod();
protected:
int mProtectedInt;
private:
int mPrivateInt;
};
The following is its implementation file...
//Super.cpp file
#include "Super.h"
Super::Super(){};
void Super::someMethod()
{
cout << "This is Super’s version of someMethod()." << endl;
}
The following is the derived class...
//Sub.h file
#include "Super.h"
using namespace std;
class Sub : public Super
{
public:
Sub();
virtual void someMethod();
};
The following is its cpp file.
//Sub.cpp file
#include "Sub.h"
Sub::Sub(){};
void Sub::someMethod()
{
cout << "This is Sub’s version of someMethod()." << endl;
}
---
Now here is the driver program
//TestIn.cpp
#include "Sub.h"
int main(){
Super mySuper;
mySuper.someMethod();// Calls Super’s version of someMethod().
Sub mySub;
mySub.someMethod(); // Calls Sub’s version of someMethod()
Super& ref = mySub;
ref.someMethod();// Calls Sub’s version of someMethod()
return 1;
}
---
As can be seen all of my functions/methods are defined in the .cpp file. I build it like this...
g++ -Wall Super.cpp Sub.cpp TestIn.cpp -o TestIn
/tmp/cc0jZLLp.o: In function `main':
TestIn.cpp:(.text+0x71): undefined reference to `Sub::Sub()'
collect2: ld returned 1 exit status
I also tried compiling each files by doing this
g++ -Wall -c Super.cpp Sub.cpp TestIn.cpp
Then linking doing this..
g++ -Wall Super.o Sub.o TestIn.o -o TestIn
TestIn.o: In function `main':
TestIn.cpp:(.text+0x71): undefined reference to `Sub::Sub()'
collect2: ld returned 1 exit status
I am at my wits end and I do not know what I am doing wrong. I will be very thankful if you can help me with my difficulty.
Trumpgeek
- 09-04-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
It could be the line
is being misinterpreted as a declaration and not an implementation. Take out the trailing semicolon and see if that works. Note that you did the same thing with Super::Super(){}; Normally, the trailing semicolon should be discarded by the compiler. The other issue is likely that you didn't link in Sub.o into your main program. Try building Sub.cpp and Super.cpp into .o files first, and then build the test program using this build line:Code:Sub::Sub(){};
.Code:c++ -Wall TestIn.cpp Sub.o Super.o -o TestIn
.
.
Doh! I see you already tried that... Sorry, my bad.Last edited by Rubberman; 09-04-2009 at 05:07 PM.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-04-2009 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
So, what happens if you make the constructor inline in Sub.h instead of implementing it in Sub.cpp?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-04-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
One last thing comes to mind is in your second attempt to build/link separately you specify the component .o files after the TestIn.o in the link line. It may be that the linker has "forgotten" (another word for optimized out) about Sub() since it doesn't see it being used until after it sees TestIn.o. So, try this:
This tells the linker to look for unresolved references as it processes each object file in turn.Code:g++ -Wall TestIn.o Sub.o Super.o -o TestIn
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-04-2009 #5Just Joined!
- Join Date
- Sep 2009
- Posts
- 2
Hey Rubberman,
I did this...
g++ -Wall -c TestIn.cpp Sub.cpp Super.cpp
then this...
g++ -Wall TestIn.o Sub.o Super.o -o TestIn
and it worked!!!!
You are my hero, thanks. I did not have to (BTW) in line the constructors.
Thank you, thank you, thank you and I thank you....
Trumpgeek


Reply With Quote