开发者

c++ inheritance problem "undefined reference to"

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 :(.


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


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.


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜