6

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?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Daniel
  • 6,595
  • 9
  • 38
  • 70

2 Answers2

9

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;
};
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • @Daniel, also when using access specifier for inheritance you don't need `:`. You may also want to learn about default access specifiers for `class` and `struct` are `private` and `public` respectively (unlike default scope in Java). – iammilind Sep 28 '11 at 04:29
  • If I don't want to put the function in a class? how should I write it? – STF Jun 20 '17 at 10:39
  • @STF, in such case you may put that function simply in an open space (i.e. under no scope) OR put it inside a `namespace` as suggested in the Qn. But you cannot have any access specifier associated with it once the function is outside a class. – iammilind Jun 20 '17 at 11:37
0

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.