在c++中练习继承时,我一直遇到以下错误:
base1.o: 在函数“Base1::Base1()”中: base1.cpp:(.text+0x75): 未定义对“Base2::Base2()”的引用 base1.o: 在函数“Base1::Base1()”中: base1.cpp:(.text+0xa5): 未定义对“Base2::Base2()”的引用 collect2: ld 返回了 1 个退出状态 make: * [test] 错误 1
我删除了所有不必要的代码,只留下以下内容:
base1.h:
这可能是一个简单的问题,但我已经花了大约2个小时在谷歌上寻找解决方案,但没有找到任何有用的信息,所以我会非常感激任何帮助。
大家好,
非常感谢你们迄今为止提供的答案。
我已将base2.cpp更改为:
base1.o: 在函数“Base1::Base1()”中: base1.cpp:(.text+0x75): 未定义对“Base2::Base2()”的引用 base1.o: 在函数“Base1::Base1()”中: base1.cpp:(.text+0xa5): 未定义对“Base2::Base2()”的引用 collect2: ld 返回了 1 个退出状态 make: * [test] 错误 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”有关,但我不知道如何正确使用它 :(。
} 但是我仍然得到相同的错误 :( - Anselm