zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    iOS中过滤html文档中的标签
    十六进制函数转换UIColor对象
    vue使用echarts
    vue打包部署
    charels代理跨域访问接口
    vue 使用highcharts
    vue配置跨域
    命令行
    安装nvm
    vsCode个人设置
  • 原文地址:https://www.cnblogs.com/liaomin416100569/p/9331641.html
Copyright © 2011-2022 走看看