I don’t understand why this fails to compile:
#include <SomeType.h> // has a namespace called SomeNamespace
class MyApplication;
int main(...)
{
...
MyApplication application;
...
}
class MyApplication : public SomeNamespace::SomeType {
...
};
As it stands I’m getting this error from g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
../fix-protocol/main.cpp:44: error: aggregate ‘MyApplication application’ has incomplete type and cannot be defined
In
mainyou’re instantiating an object of typeMyApplication, which is still an incomplete type; you can’t do that, since the compiler doesn’t know anything about it yet (it would need to know e.g. how big is it, if it has any constructor, …).To solve your problem, you have to define
MyApplicationbefore instantiating objects of that type. Usually you place the class definition in a separate header with its name, that will be#included in any file that needs it.Forward declarations, instead, in general are used to break cyclic dependencies and other scenarios of that kind; all they say is “there’s a class named like that”, but they create an incomplete type, so they can be used only to declare variables of their type, not to define them.