yu***@gmx.de wrote:
Hello gurus,
I stuck in following: how can I do forward declaration if the forward
declared class is in some namespace?
something like
// header
class std::string; // approach#1
namespace std
{
class string; // approach#2
};
struct A
{
A();
~A();
std::string * p;
};
both approaches does not compiled.
So, How can I do it?
Thanks
Approach #2 works in general. See this example, which compiles just fine
(compiling using cygwin: cygming special):
namespace ns
{ struct A; }
struct B
{ ns::A* a; };
namespace ns
{ struct A {}; }
int main()
{
B b; // "unused variable", I know
return 0;
}
You problem is that you got the type of std::string wrong. So, you
create a conflict between you saying
namespace std { class string; }
and the <string> header saying something else.
Consider this example, which also works:
// header.hpp
namespace std
{
template<typena me _CharT, typename _Traits, typename _Alloc> class
basic_string;
typedef basic_string<ch ar> string;
}
struct B
{ std::string* s; };
// main.cpp
#include <string>
#include "header.hpp "
int main()
{
B b;
return 0;
}
In the forward declaration I use the declaration which is also used in
the <string> library header.
Sadly, the above is still implementation-dependent. I would appreciate
some enlightenment how to do this really non-implementation-dependent.
Gabriel