C++头文件定义与实现

c++习惯将定义放在头文件中  实现放在cpp文件中

容易出现的错误

错误1:

error C2143: syntax error : missing ';' before 'PCH creation point'

该错误是因为 头文件类定义结束时要加 ;结尾 不然会出现上错误

错误2:

error LNK2001: unresolved external symbol "public: void __thiscall Person::eat(void)" (?eat@Person@@QAEXXZ)
Debug/HeadC.exe : fatal error LNK1120: 1 unresolved externals

该错误是因为 头文件定义了方法 而cpp文件没有实现

实现

#ifndef Person_H
#define Person_H
class Person{
public:
 Person();
 void eat();
 void walk();
 void heart();
};
#endif

#include "Person.h"
#include <iostream.h>
Person::Person(){
};

void Person::eat(){
 cout<<"person eat"<<endl;
};
void Person::heart(){
 cout<<"person heart"<<endl;
};
void Person::walk(){
 cout<<"person walk"<<endl;
};

#include "Person.h"
#ifndef Chinese_H
#define Chinese_H
class Chinese : public Person{

public:
 void heart();

};
#endif

#include "Chinese.h"
#include <iostream.h>
void Chinese::heart()
{
 cout<<"chinese heart"<<endl;
}

原文地址:https://www.cnblogs.com/liaomin416100569/p/9331640.html