Forward decleration of enumerations and typedefs in a NameSpace

K

kasiyil

Hello,
I have a compilation problem about namespaces:

I have a namespace X in which I declera a type definition:

In X.h file i write:

namespace X {
typedef unsigned int WindowHandle_t;
}

In a class which is in a different namespace ( Y ), I must use the type
definition I declared in namespace X.
MyClass.h file:

Y::MyClass {
....
private:
X::WindowHandle_t handle;
....
}

And the code doesn't compile. I should forward declare this handle type
definition but forward decleration is the same as writing the same
thing that I wrote in X.h file. When I write

In MyClass.h file :

namespace X {
typedef unsigned int WindowHandle_t;
}


Y::MyClass {
....
private:
X::WindowHandle_t handle;
....
}

The code compiles. But as I stated, I do not want to write same type
definition twice in a code. Do you have any suggestions?

Thanks...
 
I

Ian Collins

kasiyil said:
Hello,
I have a compilation problem about namespaces:

I have a namespace X in which I declera a type definition:

In X.h file i write:

namespace X {
typedef unsigned int WindowHandle_t;
}

In a class which is in a different namespace ( Y ), I must use the type
definition I declared in namespace X.
MyClass.h file:

Y::MyClass {
....
private:
X::WindowHandle_t handle;
....
}

And the code doesn't compile. I should forward declare this handle type
definition but forward decleration is the same as writing the same
thing that I wrote in X.h file. When I write
Have you included X.h?
 
J

Jens Theisen

kasiyil said:
I included it after MyClass.h. There is no way to include it before
MyClass.h .

Why is there no way?

You need to include it from within MyClass.h and before the definition
of MyClass, or move it to another header and do so with the new header.

Forward declaration has nothing to do with this. Even if you could
forward declare a typedef, which unfortunately you don't, it wouldn't
help you because

1. You're returning the type by value so it has to be complete anyway
2. For the sake of clean code you will probably want to put the forward
delcaration in a header anyway.

Jens
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top