“基类未定义”。

问题描述:

我得到了一个错误:“基类未定义”。

error C2504: 'employee' : base class undefined.

我使用Visual Studio 2010中

这是我第一次用C与传承工作++,我想不出我在做什么错误。当我尝试创建一个派生另一个类的类时,它说父类是未定义的,即使我已经包含父类的头文件。可能是什么问题呢?

Main.cpp的:

#include <string> 
#include <iostream> 
using namespace std; 

#include "manager.h" 

int main() 
{ 
manager ian; 
ian.getdata(); 
ian.putdata(); 

return 0; 
} 

Manager.h:

#pragma once 
#include "employee2.h" 
#include "student.h" 

class manager : private employee2, private student //Error happens here 
{ 
    //Stuff 
}; 

Student.h:

#pragma once 
class student 
{ 
    //Stuff 
}; 

Employee2.h:

#pragma once 
#include "employee.h" 
class employee2 : employee 
{ 
    //Stuff 
}; 

它说学生类和employee2类都是未定义的。此外,employee2类继承了一个名为employee的类,它也会得到相同的错误。我究竟做错了什么?

编辑:这是我的student.cpp文件。所有其他.cpp文件,我得到的错误看起来类似于这个。

#include "student.h" 

void student::getedu(){ 
cout << "Enter name of school or university: "; 
cin >> school; 
cout << "Enter highest degree earned \n"; 
cout << "(Highschool, Bachelor's Master's, PhD)"; 
cin >> degree; 
} 

void student::putedu() const{ 
cout << "\n School or university: " << school; 
cout << "\n Highest degree earned: " << degree; 
} 
+3

请显示完整的错误消息。 – 2012-04-08 20:33:00

+1

1)我不会使用“私人”继承,除非你真的,真的确定这就是你想要的。请更改两个子类:'班级经理:公务员2,公立学生'和班级员工2:公务员'2)丢失“#pragma once”。替代'#ifdef employee2_h ...'等 – paulsm4 2012-04-08 20:42:40

+1

'class employee'?...我看到'employee2' ...没有'employee' ...显示'employee.h'? – Marlon 2012-04-09 00:51:40

我不相信你根据你显示的代码得到“x is undefinied”。其实,看来你使用的是错误的#include

#include employee.h 

应该

#include "employee.h" 

除此之外,除了缺少employee类,你提了,manager没有在你的例子getdata和“putdata,你的代码将编译好的。

+0

对不起,我在我的文章中手动输入了该部分。代码中的所有内容都是正确的。 – FunkyVerb 2012-04-08 20:47:14

+0

在这种情况下,您的代码使用Visual Studio 2010可以很好地编译。您可以发布您正在使用的错误和编译器吗?如果它仍然说“x未定义”,那么你可能会错过。来自你的项目的cpp文件,但你的代码使用VS2010编译得很好。 – josephthomas 2012-04-08 20:52:23

+0

错误C2504:'employee':基类未定义。 Visual Studio 2010. – FunkyVerb 2012-04-08 20:55:17

确保每个文件末尾都有空格 在编译过程中,文件被连接在一起,并且您的编译指示可能因为像t他:

}#pragma once 

'}'来自之前包含的文件。

这可能是一个原因。此外,代码看起来像它应该是可编译的。

+0

有人可以证实这可能发生?编译器似乎很容易避免这样做 – 2013-06-04 01:50:46

+0

为什么预编译器会插入一些内容,用户可能不想要什么?我看到了代码,这被用作一个功能。不记得为什么,是一个“聪明”程序员的丑陋黑客。这用于产生更多的伤害而不是使用。因此,一些编译器将文件中缺失的结束行报告为错误。 – 2016-11-22 17:06:54