C++ 错误 C2143:语法错误:缺少“;” 在“使用”之前

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

error C2143: syntax error : missing ';' before 'using'

c++static

提问by Amin Khormaei

this is my header:

这是我的标题:

#ifndef HEADER_H
#define HEADER_H

class Math
{
    private:
        static enum names {amin = 27 , ali = 46};

    public:
        static void displayMessage();

}


#endif // HEADER_H

and this is the header definition:

这是标题定义:

#include <iostream>
#include <iomanip>
#include "Header.h"

using namespace std;

void Math::displayMessage()
{
    cout<<amin<<setw(5)<<ali<<endl;
}

and this is the main:

这是主要的:

#include <iostream>
#include "Header.h"

using namespace std;

enum Math::names;

int main()
{
    Math::displayMessage();
}

i got these errors:

我收到了这些错误:

error C2143: syntax error : missing ';' before 'using'  
error C2143: syntax error : missing ';' before 'using'  

one of them is for main and the other is for header definition, i have encountered several time in my programming, could explain that for me in this situation,

其中一个用于 main ,另一个用于标头定义,我在编程中遇到过几次,可以在这种情况下为我解释,

please help me

请帮我

best regards

此致

Amin khormaei

阿明·霍迈伊

回答by Mark Garcia

After preprocessing, your source code[1]for your "header definition" becomes like

预处理后,“标头定义”的源代码[1]变得像

// iostream contents

// iomanip contents


class Math
{
    private:
        static enum names {amin = 27 , ali = 46};

    public:
        static void displayMessage();

}

using namespace std;

void Math::displayMessage()
{
    cout<<amin<<setw(5)<<ali<<endl;
}

Let's now see error C2143: syntax error : missing ';' before 'using'. Where is usingin the above code? What is it before using?

现在让我们看看error C2143: syntax error : missing ';' before 'using'using上面的代码在哪里?之前是什么using

}
^ This    

using namespace std;

Because of the part of the error that says missing ';', we must add that missing ;.

由于错误的一部分missing ';',我们必须添加缺少的部分;

};
 ^


[1]More precisely called a "translation unit".

[1]更准确地称为“翻译单元”。

回答by Mike Seymour

You're missing a ;after the definition of class Math.

你缺少;的定义之后class Math

回答by Lundin

missing ';' before 'using'

丢失的 ';' 在“使用”之前

Just read what it tells you. There is a missing ; before using. Then look at your code, where did you use using? (the compiler likely told you the line)

只需阅读它告诉您的内容。有一个缺失;之前using。然后看看你的代码,你在哪里使用的using?(编译器可能会告诉你这条线)

#include "Header.h"

using namespace std;

What's before using? The header include.

之前是using什么?标题包括。

The compiler most likely goes through your code in a linear manner, so what it did when it saw #include "Header.h"was to go through that file. Meaning that the error will be the very end of "Header.h". And indeed, there is a missing ; at the end of the class declaration, just like the compiler told you.

编译器很可能以线性方式遍历您的代码,因此它在看到时所做的#include "Header.h"就是遍历该文件。这意味着错误将是“Header.h”的最后。事实上,有一个缺失; 在类声明的末尾,就像编译器告诉你的那样。