Tarquila Tarquila - 3 months ago 9
C++ Question

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

In a C++ class declaration:

class Thing
{
...
};


why must I include the semicolon?

Answer

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.