c++继承问题 "undefined reference to"

标签 c++ inheritance

在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/

相关文章:

c++ - sf::Font::loadFromFile 使程序崩溃

java - 我可以从抽象父类(super class)的方法访问在子类上定义的静态成员变量吗?

JSF 1.2 托管 bean 继承不起作用

c++ - "hour"计数器不会重置,程序再次运行后

c++ - 在每台计算机上生成相同的随机排列

c++ - 从文件中读取行并存储在单独的字符串变量中

java - java中类图的继承关系?

web-services - 从多个Web服务处理相同的类定义

javascript - 为什么我不能从自身内部重新定义函数的原型(prototype)

c++ - 什么负责删除我的指针?