Expected `:' before ‘int’

From Cppsyntax

Jump to: navigation, search

One way to get the error

 expected `:' before ‘int’

is to forget the colon after the privacy indicator in your class declaration.

Java doesn't need a colon; C++ does. Additionally, it is normal and customary to declare things public/private/protected all in a group.

WRONG

class Foo
{
  public int getOne()  // missing :
  {
    return 1;
  }
  public int getTwo()  // don't need to repeat
  {
    return 1 + getOne();
  }
};

RIGHT (more common)

class Foo
{
public:             // say "public" once here
  int getOne()  
  {
    return 1;
  }
  int getTwo()  
  {
    return 1 + getOne();
  }
};

You don't have to combine public/private/protected declarations, but it's not common to do so. Here's an example of a less-common way to do it:

VALID (less common)

class Foo
{
  public: int getOne()  
  {
    return 1;
  }
  public: int getTwo()  
  {
    return 1 + getOne();
  }
};
Personal tools