简体   繁体   English

C++ 中的基类未定义错误

[英]Base class undefined error in C++

---UPDATED --- - -更新 - -

I have problems when including headers and cpp files in my project, so here are the files: Person.h我在项目中包含头文件和 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

Teacher which extends Person Teacher.h扩展 Person Teacher.h 的教师

#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

Then here is Teacher.cpp:然后是Teacher.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;
}

The other class which extends Person is Student and since it's similar to teacher i won't poste it here.另一个扩展 Person 的类是 Student ,因为它与老师相似,所以我不会在这里发布它。 My question is what am i doing wrong to get all these errors on the screenshot: http://s14.postimage.org/45k08ckb3/errors.jpg我的问题是我做错了什么才能在屏幕截图上显示所有这些错误: http : //s14.postimage.org/45k08ckb3/errors.jpg

The problem is your incorrect treatment of stdafx.h file.问题是你对stdafx.h文件的错误处理。 In MSVC compilers, when precompiled headers are enabled, everything before #include "stdafx.h" line is ignored .在 MSVC 编译器中,当启用预编译头时, #include "stdafx.h"行之前的所有内容都将被忽略

Firstly, stop including stdafx.h into header ( .h ) files.首先,停止将stdafx.h包含到头文件 ( .h ) 中。 stdafx.h is supposed to be included into implementation ( .cpp ) files. stdafx.h应该包含在实现 ( .cpp ) 文件中。 In your case, #include "stdafx.h" should be placed into Person.cpp and Teacher.cpp , not into Person.h and Teacher.h .在您的情况下, #include "stdafx.h"应该放入Person.cppTeacher.cpp ,而不是放入Person.hTeacher.h

Secondly, either disable precompiled headers in your project, or make sure that #include "stdafx.h" is always the very first meaningful line in each of your implementation files.其次,要么禁用项目中的预编译头,要么确保#include "stdafx.h"始终每个实现文件中第一个有意义的行。 All other #include directives should go after #include "stdafx.h" , not before.所有其他#include指令应该#include "stdafx.h" ,而不是之前。

In your header files put;在你的头文件中放;

 #ifndef CLASSNAME_H
 #define CLASSNAME_H

At the top of the file, after include statements, before the class declaration.在文件的顶部,在include语句之后,在类声明之前。 Put

#endif

At the bottom of the file after all the code.在所有代码之后的文件底部。 This ensures that the class is only defined once.这确保类只定义一次。 Having multiple includes for the same header file often causes linking issues.对同一个头文件有多个包含通常会导致链接问题。

Just put into the header a guard只需将一名后卫放入头球

ie IE

#ifndef _THIS_FILENAME
#define _THIS_FILENAME

wibble etc


#endif

EDIT编辑

Forgot to mention use forward declarations - saves on the expense of recompilation.忘记提及使用前向声明 - 节省重新编译的费用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM