在c++中练习继承时,我不断收到以下错误:
base1.o:在函数 Base1::Base1()' 中:
base1.cpp:(.text+0x75): undefined reference
Base2::Base2()'
base1.o:在函数 Base1::Base1()' 中:
base1.cpp:(.text+0xa5): undefined reference
Base2::Base2()'
collect2: ld 返回 1 退出状态
make: * [测试] 错误 1
我删除了所有不必要的代码,所以只剩下这个:
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;
}
这可能是一个简单的问题,但我已经花了大约 2 个小时在谷歌上寻找解决方案,但没有找到任何解决方案,所以我将不胜感激。
你好,
感谢大家到目前为止的回答。
我变了 base2.cpp 到:
#include <QStringList>
#include <QTextStream>
#include "base2.h"
QTextStream cout(stdout);
QTextStream cin(stdin);
Base2::Base2() {
cout << "\nB2\n\n" << flush;
}
但是,我仍然得到同样的错误。我认为它一定与“#include”有关,但我不知道该怎么做:(。
最佳答案
可能是打字错误,但在 base2.cpp 中应该是 Base2::Base2()
而不是 Base1::Base1()
关于c++继承问题 "undefined reference to",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5306187/