3

While practicing inheritance in c++, I kept on getting the following error:

base1.o: In function Base1::Base1()': base1.cpp:(.text+0x75): undefined reference toBase2::Base2()' base1.o: In function Base1::Base1()': base1.cpp:(.text+0xa5): undefined reference toBase2::Base2()' collect2: ld returned 1 exit status make: * [test] Error 1

I removed all unnecessary code, so that only this is left:

base1.h :

#include "base2.h"
#ifndef BASE1_H_
#define BASE1_H_

class Base1 : public Base2 {  

public:
Base1();   
};

#endif

base1.cpp :

#include <QStringList>
#include <QTextStream>
#include "base1.h"
#include "base2.h"

QTextStream cout(stdout);
QTextStream cin(stdin);

Base1::Base1() : Base2() {
cout << "\nB1\n\n" << flush;
}

base2.h :

#ifndef BASE2_H_
#define BASE2_H_

class Base2 {

public:
Base2();
};

#endif

base2.cpp :

#include <QStringList>
#include <QTextStream>
#include "base2.h"

QTextStream cout(stdout);
QTextStream cin(stdin);


Base1::Base1() {
cout << "\nB2\n\n" << flush;   
}

child.cpp :

#include <QStringList>
#include <QTextStream>
#include "base1.h"
#include "base2.h"

QTextStream cout(stdout);
QTextStream cin(stdin);


Base1::Base1() {
cout << "\nB2\n\n" << flush;
}

This is probably an easy problem, but I have been spending like 2 hours looking for a solution on google and didn't find anything, so I would appreciate any help.


Hi,

thanks everyone for your answers so far.

I have changed base2.cpp to:

#include <QStringList>
#include <QTextStream>
#include "base2.h"

QTextStream cout(stdout);
QTextStream cin(stdin);


Base2::Base2() {
cout << "\nB2\n\n" << flush;
}

however, I still get the same error. I think it must have something to do with the "#include", but I don't know how to do it right :(.

4 Answers 4

2

Probably a typo but in base2.cpp it should say Base2::Base2() instead of Base1::Base1()

Sign up to request clarification or add additional context in comments.

Comments

2

You have defined Base1::Base1() in three .cpp files and have not defined Base2::Base2() at all.

You need to define each member function exactly once.

Comments

2

You have not implemented

Base2::Base2();

But you've used it. That's why its undefined reference (link error)

In base2.cpp replace Base1::Base1() with Base2::Base2() and it will fix.

Use smarter more clear names to prevent these bugs.

15 Comments

you are right, I should use more clear names, I thought it would be ok for this little test...
what i suggest to my own students it to erase all the code and do it again with proper indentation, naming and etc. you have included some files inside IFDEF blocks and some outside it! it might cause duplicates.
@AbiusX: What? His use of #ifdef header guards is perfect. They should not be found in the .cpp files. What do you see wrong with it? Also, your style of teaching prohibits students from learning basic debugging, which is quite a shame.
@Tomalak includes should be made inside the guard not outside it.
@AbiusX: It doesn't matter at all, because the headers that you're including have their own guards.
|
2

Very simple: you declare Base2() but never define it. You must, even if it's empty... or don't declare it at all and an empty one will be generated for you.

Perhaps Base1::Base1() in base2.cpp should read Base2::Base2()?

child.cpp shouldn't have definitions for any of them.

Edit You've said you're still having problems.

I'm going to assume that you paid attention to the above and removed the extraneous definition of Base1::Base1() from child.cpp.

How are you building your project? It should be like:

 g++ base1.cpp base2.cpp child.cpp -o myProgram

or like:

 g++ base1.cpp -o base1.o
 g++ base2.cpp -o base2.o
 g++ child.cpp -o child.o
 g++ base1.o base2.o child.o -o myProgram

(which is often the result of working with a makefile or other automated build processes).

2 Comments

thanks, so I changed base2.cpp to Base2::Base2() { cout << "\nB2\n\n" << flush; } but I keep getting the same error :(,
@Anselm: The error must be different. Did you read what I said about child.cpp and follow the link? See my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.