There is a difference between the declaration of a class name --- e.g.,
class Foo;
, and the definition of a class, e.g.,
class Foo {};
.
In the first case,
class Foo;
says that there is a type named
Foo
, but nothing more -- the compiler has no idea what a
Foo
is; it has
incomplete type only until its definition is visible.
In the second case,
class Foo {...};
says that there is a type name
Foo
along with what makes up any particular
Foo
.
Once that is visible,
Foo
has
complete type.
Note that a definition of a class does not that imply the definition of its member functions. Those probably shouldn't be in the header file, because they'll slow down compilation and will arguably make the header file harder to read.
Those member function definitions can go in another translation unit (TU, think "a source file and everything included by it"); the compiler just has to know each member function exists before it sees it.
Example:
Foo.hxx
1 2 3 4 5 6 7 8 9
|
# if ! defined PROJECT_HEADER_FOO_HXX
# define PROJECT_HEADER_FOO_HXX
class Foo {
public:
int bar(int baz);
}
# endif
|
Foo.cxx
1 2 3 4
|
# include "foo.hxx"
int Foo::bar(int baz) {
return baz + 1;
}
|