> I have the definitions of classes A and B in a header file. Class A has a[color=blue]
> private member of type B. Class B is defined after class A in the header
> file. I place a forward declaration of class B above class A but VS.NET
> still complains with:
>
> error C2079: 'TextDB::TextDB::database_version' uses undefined class
> 'TextDB::TextDB_Version'
>
> namespace TextDB
> {
> class TextDB_Version;
>
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };
>
> class TextDB_Version
> {
> [...]
> };
>
> } // namespace TextDB
>
> Is this because this is in a header file? I'm confused.[/color]
C++ obeys parsing rules that behave as-if the compiler were only allowed to
"know" things that have been defined "above" where they are used.
(Also, C++'s core language parses after the preprocessor commands, so the
location of a declaration "in a header file" is irrelevant.)
As a rubric, think of the definition of a class (the {} part) as declaring a
classes "size", and think of the definition only working if the compiler
knows the size of each component object.
So, at this line...
[color=blue]
> class TextDB
> {
> private:
> TextDB_Version database_version;
> };[/color]
The compiler is trying to determine the size of TextDB. But it does not yet
know the size of TextDB_Version. So it can't define TextDB in terms of
TextDB_Version, which is not defined yet.
The solution is to put TextDB_Version above TextDB. C++'s object
construction rules take precedence - slightly - over any grandiose ideals of
perfectly encapsulated objects.
--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces