C++ - 错误:在“使用”之前预期不合格的 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2965684/
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-28 11:36:59  来源:igfitidea点击:

C++ - Error: expected unqualified-id before ‘using’

c++inheritance

提问by F. P.

I have a C++ program and when I try to compile it it gives an error:

我有一个 C++ 程序,当我尝试编译它时,它给出了一个错误:

calor.h|6|error: expected unqualified-id before ‘using'|

Here's the header file for the calorclass:

这是calor该类的头文件:

#ifndef _CALOR_
#define _CALOR_

#include "gradiente.h"

using namespace std;

class Calor : public Gradiente
{
public:
    Calor();
    Calor(int a);
    ~Calor();

    int getTemp();
    int getMinTemp();
    void setTemp(int a);
    void setMinTemp(int a);

    void mostraSensor();
};
#endif

Why does this happen?

为什么会发生这种情况?

This class inherits from gradiente:

这个类继承自gradiente

#ifndef _GRADIENTE_
#define _GRADIENTE_

#include "sensor.h"

using namespace std;

class Gradiente : public Sensor
{
protected:
    int vActual, vMin;
public:
    Gradiente();
    ~Gradiente();
}
#endif

Which in turn inherits from sensor

依次继承自 sensor

#ifndef _SENSOR_
#define _SENSOR_

#include <iostream>
#include <fstream>
#include <string>

#include "definicoes.h"

using namespace std;

class Sensor
{
protected:
    int tipo;
    int IDsensor;
    bool estadoAlerta;
    bool estadoActivo;
    static int numSensores;
public:
    Sensor(/*PARAMETROS*/);
    Sensor(ifstream &);
    ~Sensor();

    int getIDsensor();
    bool getEstadoAlerta();
    bool getEstadoActivo();

    void setEstadoAlerta(int a);
    void setEstadoActivo(int a);

    virtual void guardaSensor(ofstream &);
    virtual void mostraSensor();
    // FUN??O COMUM
    /* virtual int funcaoComum() = 0;
    virtual int funcaoComum(){return 0;};*/
};
#endif

For completeness' sake, here's definicoes.h

为了完整起见,这里是 definicoes.h

#ifndef _DEFINICOES_
#define _DEFINICOES_

const unsigned int SENSOR_MOVIMENTO = 0;
const unsigned int SENSOR_SOM = 1;
const unsigned int SENSOR_PRESSAO = 2;
const unsigned int SENSOR_CALOR = 3;
const unsigned int SENSOR_CONTACTO = 4;

const unsigned int MIN_MOVIMENTO = 10;
const unsigned int MIN_SOM = 10;
const unsigned int MIN_PRESSAO = 10;
const unsigned int MIN_CALOR = 35;
#endif

What am I doing wrong?

我究竟做错了什么?

回答by

There is a semicolon missing at the end of this class:

这个类的末尾缺少一个分号:

class Gradiente : public Sensor
{
protected:
    int vActual, vMin;
public:
    Gradiente();
    ~Gradiente();
}                       // <-- semicolon needed after the right curly brace.

Also, the names of your include guards are illegal. Names that begin with an underscore and an uppercase letter are reserved for the C++ implementation (as are names containing a double underscore) - you are not allowed to create such names in your own code. And you should never use:

此外,你的包含守卫的名字是非法的。以下划线和大写字母开头的名称保留用于 C++ 实现(与包含双下划线的名称一样)- 不允许在您自己的代码中创建此类名称。你永远不应该使用:

using namespace std;

in a header file. And lastly, the destructor in your Sensor base class should almost certainly be made virtual.

在头文件中。最后,您的 Sensor 基类中的析构函数几乎肯定应该是虚拟的。

回答by George

In gradiente.h you forgot the semicolon at the end of your class declaration.

在gradiente.h 中,您忘记了类声明末尾的分号。

You need this:

你需要这个:

class Gradiente : public Sensor
{
    protected:
        int vActual, vMin;
    public:
        Gradiente();
        ~Gradiente();
};  

See the added semicolon?

看到添加的分号了吗?

回答by wheaties

You forgot to leave the last semi colon on the closing brackets, };, on the gradiente class.

您忘记};在gradiente 类的右方括号, 上留下最后一个分号。