Dear all, this might as well be a C++ error but here it goes: I have the following concept:
namespace name {
namespace space1 {
class Friend {
};
};
};
namespace name {
namespace space2 {
MyClass {
friend ::name::space1::Friend;
};
};
};
For some reason I get Error: class, struct, union or type name::space1 not defined (in the line with the friend declaration) in rootcint. I think I am missing something simple. Any ideas? Should I #include the Friend.h file in any case? I would like to avoid this. Thanks,
OK, after re-reading the between-the-lines part of your posting: yes you should #include Friend.h, or properly forward declare it. A friend decl is NOT a fwd decl.
using name::space1::Friend;
class name::space1::Friend;
with/out leading ::, in and out of the name::space2 scope and the MyClass scope and I still get the same error. Could you post the working example maybe? I really think I am missing sth simple. #including Friend.h is not really an option actually Thanks,
Thanks, this works as expected. I was under the wrong impression that name::space::ident and namespace name { namespace space { ident; } } are the same. Do you have any specific source on this information (e.g. a reference in the C++ standard or a book), refering to the conditions of friend declarations? I was not able to find anything on a quick look in the 1997 standard. Thanks,
[quote]Thanks, this works as expected. I was under the wrong impression that name::space::ident and namespace name { namespace space { ident; } } are the same. Do you have any specific source on this information (e.g. a reference in the C++ standard or a book), refering to the conditions of friend declarations? I was not able to find anything on a quick look in the 1997 standard. Thanks,
filimon
[/quote]
No, they are not the same. And your problem was not the friend declaration.
I guess you have to read the clause 3 - the part, describing name lookup, and, may be, about elaborated-type-specifiers.
If you have qualified name
name1::name2::name3 compiler should know, what ‘name1’ means (so, it should be declared and let’s say, it’s a namespace), after that, compiler will look up ‘name2’ in namespace ‘name1’ etc. - so you have to declare these entities before you can use such qualified name (it’s very simplified, incomplete and inaccurate description).
But… even if you have declared them:
namespace A
{
namespace B
{
class C{};
}
}
you cannot write
class A::C; it’s an ill-formed construct, and, for example, comeau compiler will give you error like this:
and standard prohibits such declaration explicitly, read about elaborated-type-specifiers in the clause 3