Need to declare a class ( defined in cpp file ) as friend
-
Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek
-
Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek
Can you post the relevant code snippets? It's hard to tell what's going on from your description.
The difficult we do right away... ...the impossible takes slightly longer.
-
Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek
If I remember correctly you can (at least in VS 6.0) include .cpp files the same way you include header files. However it's not a very good practice. You could consider defining some kind of helper class or interface class in z.h and include this file in both x.cpp and y.h. Then you could declare z a friend of y and "channel" the function calls from x through z. Alternatively you could even derive y from z and wouldn't need a friend declaration.
The good thing about pessimism is, that you are always either right or pleasently surprised.
-
Hello, I have a namespace and class defined inside a cpp file (say x.cpp ). I want to declare this class as 'friend' inside another header (say y.h), but the header won't recognize the namespace. Now, when I put the namespace in another header (x.h) and include it in y.h, I get a re-definition error ( also in x.cpp ). I can't remove the namespace definition from x.cpp, since it has been put together by a framework for me, and I won't be able to use the framework otherwise. Does this mean I can't declare this class as friend of any other class ? Regards, Raj Abhishek
Have you already tried a forward declaration, like this:
namespace myspace {
class x; // forward declaration of class myspace::x
};class y {
friend myspace::x;
};GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)