Why must I put a semicolon at the end of class declaration in C++?

The full syntax is, essentially,

class NAME { constituents } instances ;

where “constituents” is the sequence of class elements and methods, and “instances” is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
  int bar;
  int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
  int bar;
  int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

Leave a Comment