Error: base class undefined in deriving class Son from baseclass Father
Error: base class undefined in deriving class Son from baseclass Father
我做了两个类父子,分成.h和.cpp。为什么会出现上述错误?我有另一个包含基本相同且没有错误的解决方案。抱歉,如果这是微不足道的,但我是 c++ 的新手,并且很困惑为什么它在一个解决方案中有效,但在另一个解决方案中无效。
编辑:当我 运行 程序时,终端仍然打开并显示父亲的 cout 行,错误似乎是在那之后发生的。我知道在 Son.h 中包含 Father.h 的解决方案。但是为什么它不像我写的那样工作呢?我喜欢在 cpp 文件中包含头文件的想法。
Father.h
#pragma once
class Father
{
public:
Father();
~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;
}
Son.h:
#pragma once
class Son : public Father
{
public:
void Talk();
};
Son.cpp:
#include "Father.h"
#include "Son.h"
#include <iostream>
using namespace std;
void Son::Talk()
{
cout << "I'am the son" << endl;
}
Main.cpp:
#include "Son.h"
#include <iostream>
using namespace std;
int main()
{
Son Bernd;
}
why doesn't it work the way I wrote it?
Son.cpp
编译得很好,因为它在 Son.h
的 Son
声明之前包含 Father.h
的 Father
声明。
问题出现在Main.cpp
。此处您仅包含 Son.h
中的 Son
声明。该编译单元不知道 class Father
。
确保每个 header 都包含它的所有依赖项并添加
#include "Father.h"
到Son.h
.
我做了两个类父子,分成.h和.cpp。为什么会出现上述错误?我有另一个包含基本相同且没有错误的解决方案。抱歉,如果这是微不足道的,但我是 c++ 的新手,并且很困惑为什么它在一个解决方案中有效,但在另一个解决方案中无效。
编辑:当我 运行 程序时,终端仍然打开并显示父亲的 cout 行,错误似乎是在那之后发生的。我知道在 Son.h 中包含 Father.h 的解决方案。但是为什么它不像我写的那样工作呢?我喜欢在 cpp 文件中包含头文件的想法。
Father.h
#pragma once
class Father
{
public:
Father();
~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;
}
Son.h:
#pragma once
class Son : public Father
{
public:
void Talk();
};
Son.cpp:
#include "Father.h"
#include "Son.h"
#include <iostream>
using namespace std;
void Son::Talk()
{
cout << "I'am the son" << endl;
}
Main.cpp:
#include "Son.h"
#include <iostream>
using namespace std;
int main()
{
Son Bernd;
}
why doesn't it work the way I wrote it?
Son.cpp
编译得很好,因为它在 Son.h
的 Son
声明之前包含 Father.h
的 Father
声明。
问题出现在Main.cpp
。此处您仅包含 Son.h
中的 Son
声明。该编译单元不知道 class Father
。
确保每个 header 都包含它的所有依赖项并添加
#include "Father.h"
到Son.h
.