错误:基类在从基类父派生类 Son 时未定义
Error: base class undefined in deriving class Son from baseclass Father
我做了两个类父子,分为.h和.cpp。为什么我会收到上述错误?我有另一个解决方案,包含基本相同且没有错误。抱歉,如果这是微不足道的,但我是 c++ 的新手,并且对为什么它在一个解决方案中有效而不适用于另一个解决方案感到困惑。
编辑:当我运行程序时,终端仍然打开并显示父亲的cout线,之后似乎发生了错误。我知道将父.h包括在儿子里面的souts。但是为什么它不能按照我写的方式工作呢?我喜欢在 cpp 文件中包含头文件的想法。
父亲·
#pragma once
class Father
{
public:
Father();
~Father();
};
父亲.cpp:
#include "Father.h"
#include <iostream>
using namespace std;
Father::Father()
{
cout << "I am the father constructor" << endl;
}
Father::~Father()
{
cout << "I am the father deconstructor" << endl;
}
儿子:
#pragma once
class Son : public Father
{
public:
void Talk();
};
儿子.cpp:
#include "Father.h"
#include "Son.h"
#include <iostream>
using namespace std;
void Son::Talk()
{
cout << "I'am the son" << endl;
}
主.cpp:
#include "Son.h"
#include <iostream>
using namespace std;
int main()
{
Son Bernd;
}
为什么它不能按照我写的方式工作?
Son.cpp
编译得很好,因为它包括Father.h
的Father
声明,然后再声明Son
来自Son.h
。
Main.cpp
中出现问题。在这里,您仅包括来自Son.h
的Son
声明。此编译单元不知道类Father
。
确保每个标头都包含其所有依赖项并添加
#include "Father.h"
到Son.h
.
相关文章:
- std::具有相同基类的类的变体
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 基类中的函数名称解析
- C++初始化基类
- 如何通过派生类函数更改基类中的向量
- 如何定义一个纯抽象基类
- 如何使用基类指针引用派生类成员
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如何引用基类的派生类?
- 如果基类包含双指针成员,则派生类的构造函数
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 为什么此派生对象无法访问基类的后递减方法?
- 公开最直接的基类模板名称
- 当基类是依赖类型时,这是一个缺陷吗
- 如何使基类的运算符对基类的可变参数数可见(请参阅下面的代码)?
- 模板基类中的静态变量
- C++ 继承:将子类传递给需要基类的函数并获取子类行为
- 错误:基类在从基类父派生类 Son 时未定义