C++ 错误:需要声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7578265/
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
Error: expected a declaration
提问by Daniel
So far all I have in my DecisionTree.h
file is
到目前为止,我的DecisionTree.h
文件中只有
namespace DecisionTree
{
public static double Entropy(int pos, int neg);
}
and Visual Studio is already highlighting the public
and saying
并且 Visual Studio 已经突出显示public
和说
Error: expected a declaration.
错误:需要声明。
What am I missing?
我错过了什么?
回答by iammilind
public
is an access specifier. Access specifiers are applicable only within class/struct
body and not inside namespace
. In C++ (unlike Java) it must be followed by a colon :
inside the class
body.
public
是访问说明符。访问说明符仅适用于class/struct
body 而不适用于inside namespace
。在 C++ 中(与 Java 不同),它必须:
在class
主体内跟一个冒号。
For example,
例如,
class DecisionTree { // <----- 'class' (not 'namespace')
public: // <------ access specifier
static double Entropy (int pos, int neg);
private:
int i;
};
回答by Sai Kalyan Kumar Akshinthala
It will definitely give an error, for you dint declared any class,struct, or enum and directly you have written a static function inside a namespace. So, first write a class definition inside a namespace and then a function.
它肯定会给出错误,因为您 dint 声明了任何类、结构或枚举,并且直接在命名空间内编写了一个静态函数。因此,首先在命名空间中编写类定义,然后编写函数。