C 中的基类未定义错误
Base class undefined error in C++
----更新---
我在项目中包括标题和CPP文件时遇到问题,因此这里是文件:person.h
#ifndef PERSON_H
#define PERSON_H
class Person {
private:
string firstName;
string lastName;
long NID;
public:
Person();
void toString();
string get_firstName() {
return firstName;
}
string get_lastName() {
return lastName;
}
long get_NID() {
return NID;
}
};
#endif
延长人的老师老师
#include "Person.h"
#include <iostream>
#ifndef TEACHER_H
#define TEACHER_H
class Teacher : public Person {
private:
int avg_horarium;
public:
Teacher();
void toString();
int get_avg_horarium() {
return avg_horarium;
}
};
#endif
然后是老师。cpp:
#include "Teacher.h"
using namespace std;
Teacher::Teacher() : Person() {
cout << "Enter average monthly horarium: ";
cin >> avg_horarium;
}
void Teacher::toString() {
Person::toString();
cout << "Average monthly horarium: " << avg_horarium;
}
另一个扩展人的班级是学生,因为它与老师相似,所以我不会在这里发布。我的问题是,在屏幕截图上获取所有这些错误,我做错了什么:http://s14.postimage.org/45k08ckb3/errors.jpg
问题是您对stdafx.h
文件的不正确处理。在MSVC编译器中,当启用预编译标头时,#include "stdafx.h"
行之前的所有内容都被忽略。
首先,停止将stdafx.h
加入标头(.h
)文件。stdafx.h
应该包含在实现(.cpp
)文件中。在您的情况下,#include "stdafx.h"
应放入Person.cpp
和Teacher.cpp
中,而不是Person.h
和Teacher.h
。
其次,要么禁用项目中的预编译标头,要么确保#include "stdafx.h"
始终是您每个实现文件中最有意义的行。所有其他#include
指令均应在 #include "stdafx.h"
之后进行,而不是之前。
在您的标题文件放置;
中 #ifndef CLASSNAME_H
#define CLASSNAME_H
在文件的顶部,在include
语句之后,在班级声明之前。放
#endif
在所有代码之后,在文件的底部。这样可以确保仅定义课程一次。具有相同标头文件的多个包含多个,通常会导致链接问题。
刚刚放入标题a Guard
即
#ifndef _THIS_FILENAME
#define _THIS_FILENAME
wibble etc
#endif
编辑
忘了提及使用前瞻性声明 - 节省了重新编译的费用。
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 尝试运行 wasm 函数时出现模块未定义错误
- c++中数组的未定义错误
- 基类未定义.错误 C2504
- 获取线函数未定义错误。无法在字符串中保存可验证的内容
- 对于我的 ComplexNumber 中的某些方法,我得到了一个奇怪的未定义错误引用.cpp,不过我对模板很陌生
- 类类型重定义和基类未定义错误
- C 中的基类未定义错误
- QML[未定义]错误
- 如何构建使用 OpenCV 的 XCode 6 iOS 应用程序 - 未定义错误__cplusplus
- 变量未定义错误
- 静态方法声明但未定义错误c++
- c++ cocos-2d-x未定义错误
- 基类未定义错误(C2504)
- 单例作为模板,未定义错误
- Solaris库与STLport4.6.2链接,出现与ostream相关的未定义错误