Headers cross-calling

M

Mail.To.Nathaniel

Hi all :)

I have an-easy-to-resolve (I am sure but I am obviously unable to
figure the answer out) problem for C++ gurus.

Here's the situation I have fallen into...

2 header files: H1.h & H2.h
--------------------------------------------------------------------
--- H1.h
--------------------------------------------------------------------
#include "H2.h" // <------ error at compilation (unknown type:
T1c in the T2),
// without the #include: unknown
type T2 in the T11
class T11
{
<...>
T2 method();
}

template <typename T> class T12 :
: T11
{
<...>
}

typedef T1<char> T1c;
--------------------------------------------------------------------
--- H2.h
--------------------------------------------------------------------
#include "H1.h"

class T2
{
<...>
void method(T1c);
}
--------------------------------------------------------------------

Is it possible to resolve the conflict described? Or should I find a
way to avoid this kind of cross calls?

Thanks for any help :)
 
V

Victor Bazarov

I have an-easy-to-resolve (I am sure but I am obviously unable to
figure the answer out) problem for C++ gurus.

Here's the situation I have fallen into...

2 header files: H1.h & H2.h
--------------------------------------------------------------------
--- H1.h
--------------------------------------------------------------------
#include "H2.h" // <------ error at compilation (unknown type:
T1c in the T2),
// without the #include: unknown
type T2 in the T11
class T11
{
<...>
T2 method();

Change to

class T2 method();
}

template <typename T> class T12 :
: T11
{
<...>
}

typedef T1<char> T1c;
--------------------------------------------------------------------
--- H2.h
--------------------------------------------------------------------
#include "H1.h"

class T2
{
<...>
void method(T1c);

Change to

void method(class T1c);

You don't have to avoid them. Read about forward declarations.

V
 
M

Mail.To.Nathaniel

Victor, thank you for your help :)

The first problem (T2 method) is resolved, but not the second one,
with the method (T1c). The argument is described by a typedef and not
a class. I have looked through some forward declarations documentation
pages, and it seems that it is illegal to use forward declarations
with typedef names. Am I right?
 
M

Mail.To.Nathaniel

I have replaced
void method(T1c);
by
void method(T1<char>);

Is it a good decision?
 
V

Victor Bazarov

It could be an "it-works-but-you-should-never-do-this" solution :)

OK, I took another look and think that your solution does not really
fall into that category. However, there is something to be said, of
course. 'T1c' is a typedef in the header you didn't (or couldn't)
include. Why is it a typedef? At some point the decision was made
to have a typedef instead of forcing the user type 'T1<char>'. Was
that to save typing or was there other reason? If it was to save
typing, replacing 'T1c' with 'T1<char>' in the declaration above is
perfectly OK. However, if it was because on some systems 'T1c' can
be something else (and not necessarily 'T1<char>'), and the choice
has to be governed by some #define or whatever other means, then
being explicit here may not be what you need. The latter case is,
admittedly, unlikely.

V
 
M

Mail.To.Nathaniel

It was only a user-confort choice allowing for shorter type names. So
everything seems OK now.

Thank you really very much for your help and attention.

Spasibo :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top