| re: Some problems with forward declarations
> 1. I have a class template[color=blue]
>
> template<class Base> class Matrix : public Base { /* .... */ }
>
> then
>
> typedef Matrix<UpperTriangular> UTRMatrix;
>
> My question is now: how do I refer to the class UTRMatrix in a forward
> declaration?[/color]
You forward declare Matrix and then do the typedef:
template<class Base> class Matrix;
class UpperTriangle;
typedef Matrix<UpperTriangular> UTRMatrix;
Since you can't legally typedef UTRMatrix twice in the same compilation
unit, you probably ought to put those lines in a header file somewhere. And
since that means you're already including a header, it probably makes sense
to go ahead and include the full header that you're trying to avoid with the
forward declaration...
[color=blue]
> std::string;
>
> I get the error message: "specialization of string after
> instantiation".[/color]
As stated by someone else, that isn't a forward declaration. A forward
declaration would be:
namespace std
{
class string;
}
With standard includes in particular, there is almost never a reason to
forward declare instead of just including the header. Go ahead and include
<iostream> instead.
[color=blue]
> 3. I have a class MyClass with a static const int member J.
> Now in a header file I have to refer to MyClass::J.
> I don't want to include MyClass.h so I am trying the forward
> declarations
>
> class MyClass;
> extern int MyClass::J;
>
> The compiler does not like that:
> "Struct MyClass not yet defined".
> How should I do it?[/color]
If you're going to access a member of a given class, even a static member,
you MUST include the class declaration. There's no getting around including
the header file on this one...
Perhaps you should ask yourself why you need to avoid including header
files. I realize that forward declarations are a wonderful way to help
reduce file dependencies; I use them all the time. But the need for too
many forward declarations can also be a sign that your project has too many
logical file interdependencies.
Thanks,
Darryl |