Search code examples
c++compiler-errorsaccess-specifier

Error: expected a declaration


So far all I have in my DecisionTree.h file is

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

and Visual Studio is already highlighting the public and saying

Error: expected a declaration.

What am I missing?


Solution

  • 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.

    For example,

    class DecisionTree {  // <----- 'class' (not 'namespace')
    public:  // <------ access specifier
      static double Entropy (int pos, int neg);
    private:
      int i;
    };