Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 05:19 PM
mjm
Guest
 
Posts: n/a
Default Some problems with forward declarations

Folks,

Please help me with the following problems:

********************************************

1. I have a class template

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? The following does not work

class UTRMatrix;

("Conflicting types for struct UTRMatrix").

********************************************

2. When I try a forward declaration

std::string;

I get the error message: "specialization of string after
instantiation".

**********************************************

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?


Many thanks.
  #2  
Old July 19th, 2005, 05:19 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Some problems with forward declarations

"mjm" <spyqqqdia@yahoo.com> wrote...[color=blue]
> Please help me with the following problems:
>
> ********************************************
>
> 1. I have a class template
>
> 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? The following does not work
>
> class UTRMatrix;
>
> ("Conflicting types for struct UTRMatrix").[/color]

You have already declared 'UTRMatrix'. Why do you need another
[forward] declaration?
[color=blue]
> ********************************************
>
> 2. When I try a forward declaration
>
> std::string;[/color]

That's not a forward declaration. Please restate your question.
[color=blue]
> I get the error message: "specialization of string after
> instantiation".
>
> **********************************************
>
> 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;[/color]

No. Forward declaration of members is prohibited.
[color=blue]
>
> The compiler does not like that:
> "Struct MyClass not yet defined".
> How should I do it?[/color]

You shouldn't. What are you trying to accomplish? Perhaps your
static const would be better off outside the class?

Victor


  #3  
Old July 19th, 2005, 05:19 PM
Darryl Melander
Guest
 
Posts: n/a
Default 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



  #4  
Old July 19th, 2005, 05:20 PM
mjm
Guest
 
Posts: n/a
Default Re: Some problems with forward declarations

Thanks for all the replies.
The reason I was asking is that I am trying to minimize includes in
header files.
If you include something in a header file it will be included wherever
that header file is included -- possibly needlessly.

That's why it's best to minimize the information in the header file
and instead include the stuff in the source files where it is really
needed.

For example: Matrix.h contains a class template that is fully defined
in the header and fairly large. I definitely don't want to include
Matrix.h in a header unless it is absolutely necessary. The typdef

typedef Matrix<UpperTriangular> UTRMatrix;

is in the header Matrix.h. In some other header I might need a
declaration of
UTRMatrix but not the definition. So I do not need to include Matrix.h
instead all I need is a forward declaration

class UTRMatrix;

But the compiler does not like that. For the same reason I do not
want to include <iostream> in a header if I don't have to. I always
get by with (GCC 2.95.3)

class std::ostream;

I was also thinking that it might have to be

namespace std {

class ostream;

}

but

class std::ostream works also. Why can't I do

class std::string;

The greatest nuisance is that I do not know how to forward declare
something that was defined by a typedef. I assume that this is a
common problem.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles