分享

g++报错原因分析:expected class

 键盘农夫 2018-02-13

今天写程序的时候, 遇到这样一个错误expected class-name before '{’ token  

最后发现原来是我的头文件声明没有加.

继承时不要忘记加基类的头文件

[eric] 而且基类头文件要放在派生类的前面

错误:

 class Foo: public Bar   // Foo is a subclass of Bar
 {
   // stuff
 };

正确:

 #include "Bar.h"         // this makes Bar recognized
 class Foo: public Bar   
 {
   // stuff
 };
+++++++++++++++++++++++++++++++++++++++++
my example:
class derivedCls1: public BaseCls
{
    void func1();
    void func2();
}

//
"#include BaseCls.h" <-- 这里基类的头文件要放在派生类前面, 否则就会报错
"#include derivedCls1.h"
void anotherCls::func()
{
derivedCls1 * Cls1Ptr;
Cls1Ptr->func1();
Cls1Ptr->func2();
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多