C++ 非常基本的继承:错误:“{”标记之前的预期类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11297849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:00:58  来源:igfitidea点击:

Very basic inheritance: error: expected class-name before ‘{’ token

c++inheritance

提问by Jonas

I'm trying to learn c++ and I've stumbled upon a error while trying to figuring out inheritance.

我正在尝试学习 C++ 并且在尝试找出继承时偶然发现了一个错误。

Compiling: daughter.cpp In file included from /home/jonas/kodning/testing/daughter.cpp:1: /home/jonas/kodning/testing/daughter.h:6: error: expected class-name before ‘{' token Process terminated with status 1 (0 minutes, 0 seconds) 1 errors, 0 warnings

编译:daughter.cpp 在/home/jonas/kodning/testing/daughter.cpp 包含的文件中:1:/home/jonas/kodning/testing/daughter.h:6:错误:'{' 标记之前的预期类名进程以状态 1(0 分钟,0 秒)终止,1 个错误,0 个警告

My files: main.cpp:

我的文件:main.cpp:

#include "mother.h"
#include "daughter.h"
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    mother mom;
    mom.saywhat();
    return 0;
}

mother.cpp:

妈妈.cpp:

#include "mother.h"
#include "daughter.h"

#include <iostream>

using namespace std;


mother::mother()
{
    //ctor
}


void mother::saywhat() {

    cout << "WHAAAAAAT" << endl;


}

mother.h:

妈妈.h:

#ifndef MOTHER_H
#define MOTHER_H


class mother
{
    public:
        mother();
        void saywhat();
    protected:
    private:
};

#endif // MOTHER_H

daughter.h:

女儿.h:

#ifndef DAUGHTER_H
#define DAUGHTER_H


class daughter: public mother
{
    public:
        daughter();
    protected:
    private:
};

#endif // DAUGHTER_H

and daughter.cpp:

和女儿.cpp:

#include "daughter.h"
#include "mother.h"

#include <iostream>

using namespace std;


daughter::daughter()
{
    //ctor
}

What I want to do is to let daughter inherit everything public from the mother class (=saywhat()). What am I doing wrong?

我想做的是让女儿从母类(=saywhat())继承所有公共的东西。我究竟做错了什么?

回答by Nawaz

You forgot to include mother.hhere:

你忘了在mother.h这里包括:

#ifndef DAUGHTER_H
#define DAUGHTER_H

#include "mother.h"  //<--- this line is added by me.    

class daughter: public mother
{
    public:
        daughter();
    protected:
    private:
};

#endif // DAUGHTER_H

You need to include this header, because daughteris derived from mother. So the compiler needs to know the definition of mother.

您需要包含此标头,因为daughter它源自mother. 所以编译器需要知道mother.

回答by yuklai

In daughter.cpp, switch the two lines of include. i.e.

在daughter.cpp中,切换两行include。IE

#include "mother.h"
#include "daughter.h"

What happened was that the compiler is looking into the definition of class daughterand could not find the definition of the base class mother. So it's telling you that "I'm expecting the identifier motherin front of "{" in the line

发生的事情是编译器正在查看类daughter的定义,但找不到基类的定义mother。所以它告诉你“我期待mother在行中“{”前面的标识符

class daughter: public mother {

to be a class, but I can't find it's definition!"

成为一个班级,但我找不到它的定义!”

In mother.cpp, remove the inclusion of daughter.h. The compiler does not need to know the definition of daughter.h; i.e. class mothercan be used without daughter. Adding the inclusion of daughter.hintroduces unnecessary dependency between the class definitions.

在 中mother.cpp,删除 的包含daughter.h。编译器不需要知道daughter.h;的定义。即类mother可以在没有daughter. 添加包含会daughter.h在类定义之间引入不必要的依赖关系。

On the other hand, it is always better IMHO to keep the inclusion of header in the definition of the class (.cpp) and not the declaration of the class (.h). This way it is less likely you need to resolve header inclusion nightmare when including headers that in turn include other headers which you don't have control. But many production code includes header in header. Both are correct, just need to be careful when you do that.

另一方面,恕我直言,在类的定义(.cpp)而不是类的声明(.h)中包含头文件总是更好。这样,当包含的标题又包含您无法控制的其他标题时,您不太可能需要解决标题包含的噩梦。但是许多生产代码在标题中包含标题。两者都是正确的,只是在这样做时需要小心。

回答by Luchian Grigore

First off, you have include guards in implementation files. Remove them.

首先,您在实现文件中包含了警卫。删除它们。

Second off, if you inherit from a class, you need to include the header where the class is defined.

其次,如果你从一个类继承,你需要包含定义类的头文件。

回答by chmoder

Check that #ifndefand #definein your header file is unique.

检查#ifndef#define在您的头文件中是唯一的。

#ifndef BASE_CLIENT_HANDLER_H
#define BASE_CLIENT_HANDLER_H

#include "Threads/Thread.h"

class BaseClientHandler : public threads::Thread {
public:
    bool isOn();
};
#endif //BASE_CLIENT_HANDLER_H

回答by glmdev

Not related to OP's issue, but for any other C++ learners stumbling upon this, I was getting this error for a different reason. If your parent class is templated, you need to specify the type name in the child class:

与 OP 的问题无关,但对于任何其他 C++ 学习者在这方面遇到的问题,我因不同的原因收到此错误。如果您的父类是模板化的,则需要在子类中指定类型名称:

#include "Parent.h"

template <typename ChildType>
class Child : public Parent<ChildType> { // <ChildType> is important here

};