how to have forward declaration from different namespace?

T

toton

Hi,
I have two namespace contains class InkFrame and PrefDialog
respectively. PrefDialog needs InkFrame to show the dialog over the
frame. It also stores a pointer to InkFrame inside it.
Now I want InkFrame to be forward declared in the PrefDialog header
file rather than to be included. I want to include it in the cpp file
instead. There is no harm including it in PrefDialog header, and it
works. But I want to save some compilation time. That is why I thought
to forward declare it (and all the other classes whenever possible)
instead of include. They works perfectly if both of them are from same
namespace. But fails to work if they are from different namespace.

To make it little clear, the headers are
InkFrame.hpp =>
namespace ui{
class InkFrame : public Frame{
...
} ;
}
here Frame, is a library class and necessary headers are already there.

PrefDialog.hpp =>
#include "InkFrame.hpp" //and other necessary headers.
using ui::InkFrame
namespace client{
namespace ui{
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
This code works.
Next I removed include & have forward declaration. The include and
using statement is shifted to cpp file for PrefDialog.
PrefDialog.hpp =>
//#include "InkFrame.hpp" //and other necessary headers.
//using ui::InkFrame
namespace client{
namespace ui{
class InkFrame; //forward declaration.
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
Here, the code assumes InkFrame is from client::ui (which is quite
reasonable, I think) and fails to compile the code as cpp gets it from
ui namespace.
declaring at top class ui::InkFrame also do not work, as the ui
namespace is not defined here!
also declaring like
namespace ui{
class InkFrame;
}
at the top is not working. These two cases the header do not see the
declaration even, while in the firest case it see and find a mismatch
at the cpp.

Thus, what is the common way to forward declare a class from a
different namespace?

Thanks for any advice.
abir
 
T

toton

toton said:
Hi,
I have two namespace contains class InkFrame and PrefDialog
respectively. PrefDialog needs InkFrame to show the dialog over the
frame. It also stores a pointer to InkFrame inside it.
Now I want InkFrame to be forward declared in the PrefDialog header
file rather than to be included. I want to include it in the cpp file
instead. There is no harm including it in PrefDialog header, and it
works. But I want to save some compilation time. That is why I thought
to forward declare it (and all the other classes whenever possible)
instead of include. They works perfectly if both of them are from same
namespace. But fails to work if they are from different namespace.

To make it little clear, the headers are
InkFrame.hpp =>
namespace ui{
class InkFrame : public Frame{
...
} ;
}
here Frame, is a library class and necessary headers are already there.

PrefDialog.hpp =>
#include "InkFrame.hpp" //and other necessary headers.
using ui::InkFrame
namespace client{
namespace ui{
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
This code works.
Next I removed include & have forward declaration. The include and
using statement is shifted to cpp file for PrefDialog.
PrefDialog.hpp =>
//#include "InkFrame.hpp" //and other necessary headers.
//using ui::InkFrame
namespace client{
namespace ui{
class InkFrame; //forward declaration.
class PrefDialog : public Dialog{
InkFrame* _parent;
public:
PrefDialog(InkFrame* parent);
};
}
}
Here, the code assumes InkFrame is from client::ui (which is quite
reasonable, I think) and fails to compile the code as cpp gets it from
ui namespace.
declaring at top class ui::InkFrame also do not work, as the ui
namespace is not defined here!
also declaring like
namespace ui{
class InkFrame;
}
at the top is not working. These two cases the header do not see the
declaration even, while in the firest case it see and find a mismatch
at the cpp.

Thus, what is the common way to forward declare a class from a
different namespace?

Thanks for any advice.
abir
Got the answer!
I also need to write
using ui::InkFrame or fully quilified name inside the class as type,
along with forward declaration like
namespace ui{
class InkFrame;
}
Thanks to the newsgroup ...
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top