基类未定义。错误C2504 遵循这个教程(),我在VisualStudioC++ 2017中遇到了4个错误。其中3个是相同的,只需重复“母亲”:未在子.h文件中定义基类。另一个错误是:“sayName”不是“女儿”的成员,下面是代码。我希望程序打印的内容非常简单……我希望它打印出两行“你在那里做什么?”如果你能帮我回答这个问题,那就太好了。非常感谢。 对于主文件 `#包括“stdafx.h” #包括 #包括“女儿h” #包括“母亲h” 使用名称空间std int main() { Mother pot; pot.sayName(); Daughter kettle; kettle.sayName(); int pause = 0; cin >> pause; } Mother.h #ifndef MOTHER_H #define MOTHER_H class Mother { public: Mother(); void sayName(); }; #endif Mother.cpp #include "stdafx.h" #include<iostream> #include"Daughter.h" #include"Mother.h" using namespace std; Mother::Mother() { } void Mother::sayName() { cout << "What are you doing there?" << endl; } Daughter.h #ifndef DAUGHTER_H #define DAUGHTER_H class Daughter:public Mother { public: Daughter(); }; #endif Daughter.cpp #include "stdafx.h" #include<iostream> #include"Daughter.h" #include"Mother.h" using namespace std; Daughter::Daughter() { } intmain() { 母罐; pot.sayName(); 女儿壶; catter.sayName(); int pause=0; cin>>暂停; } 妈妈 #如果没有母亲 #定义母亲 班主任 { 公众: 母亲(); void sayName(); }; #恩迪夫 母亲.cpp #包括“stdafx.h” #包括 #包括“女儿h” #包括“母亲h” 使用名称空间std; 母亲:母亲 { } void Mother::sayName(){ cout
当一个类继承另一个类时,它必须在其头中包含父类头。在您的情况下,您必须在子类头的顶部添加基类未定义。错误C2504 遵循这个教程(),我在VisualStudioC++ 2017中遇到了4个错误。其中3个是相同的,只需重复“母亲”:未在子.h文件中定义基类。另一个错误是:“sayName”不是“女儿”的成员,下面是代码。我希望程序打印的内容非常简单……我希望它打印出两行“你在那里做什么?”如果你能帮我回答这个问题,那就太好了。非常感谢。 对于主文件 `#包括“stdafx.h” #包括 #包括“女儿h” #包括“母亲h” 使用名称空间std int main() { Mother pot; pot.sayName(); Daughter kettle; kettle.sayName(); int pause = 0; cin >> pause; } Mother.h #ifndef MOTHER_H #define MOTHER_H class Mother { public: Mother(); void sayName(); }; #endif Mother.cpp #include "stdafx.h" #include<iostream> #include"Daughter.h" #include"Mother.h" using namespace std; Mother::Mother() { } void Mother::sayName() { cout << "What are you doing there?" << endl; } Daughter.h #ifndef DAUGHTER_H #define DAUGHTER_H class Daughter:public Mother { public: Daughter(); }; #endif Daughter.cpp #include "stdafx.h" #include<iostream> #include"Daughter.h" #include"Mother.h" using namespace std; Daughter::Daughter() { } intmain() { 母罐; pot.sayName(); 女儿壶; catter.sayName(); int pause=0; cin>>暂停; } 妈妈 #如果没有母亲 #定义母亲 班主任 { 公众: 母亲(); void sayName(); }; #恩迪夫 母亲.cpp #包括“stdafx.h” #包括 #包括“女儿h” #包括“母亲h” 使用名称空间std; 母亲:母亲 { } void Mother::sayName(){ cout,c++,inheritance,visual-studio-2017,C++,Inheritance,Visual Studio 2017,当一个类继承另一个类时,它必须在其头中包含父类头。在您的情况下,您必须在子类头的顶部添加#include“Mother.h”(不仅在.cpp文件中)。另一个错误是由于第一个错误而发生的,更正它应该可以解决这个问题 编写继承语法时,子类定义需要访问有关其父类的信息,原因有几个。其中之一是有关继承方法的信息,这导致了第二个错误。Move#include“Mother.h”谢谢你帮我解决我的问题。这是针对所有Visual Studio项目的吗?我问这个是因为我看的教程()没有做这个。@黑巧克力我检查过
#include“Mother.h”
(不仅在.cpp文件中)。另一个错误是由于第一个错误而发生的,更正它应该可以解决这个问题
编写继承语法时,子类定义需要访问有关其父类的信息,原因有几个。其中之一是有关继承方法的信息,这导致了第二个错误。Move
#include“Mother.h”谢谢你帮我解决我的问题。这是针对所有Visual Studio项目的吗?我问这个是因为我看的教程()没有做这个。@黑巧克力我检查过了,甚至有一些关于这个问题的评论。另外,这也适用于你写的所有C++代码(即使不是在VisualStudio中).再次感谢你帮我解决问题。