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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:10:33  来源:igfitidea点击:

Error: expected a declaration

c++compiler-errorsaccess-specifier

提问by Daniel

So far all I have in my DecisionTree.hfile is

到目前为止,我的DecisionTree.h文件中只有

namespace DecisionTree
{
     public static double Entropy(int pos, int neg);
}

and Visual Studio is already highlighting the publicand saying

并且 Visual Studio 已经突出显示public和说

Error: expected a declaration.

错误:需要声明。

What am I missing?

我错过了什么?

回答by iammilind

publicis an access specifier. Access specifiers are applicable only within class/structbody and not inside namespace. In C++ (unlike Java) it must be followed by a colon :inside the classbody.

public是访问说明符。访问说明符仅适用于class/structbody 而不适用于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 声明了任何类、结构或枚举,并且直接在命名空间内编写了一个静态函数。因此,首先在命名空间中编写类定义,然后编写函数。