Freundschaften, Klassen, Namespaces
-
Hi,
ich habe ein Problem:
#include <iostream> namespace foo { class B; class A { public: friend B; protected: int value_from_a; }; } namespace bar { class B { public: void doit (void) { foo::A a; std::cout << a.value_from_a << std::endl; // << ERROR } }; } int main (void) { return 0; }
main.cpp(24): error C2248: 'foo::A::value_from_a' : cannot access protected member declared in class 'foo::A'
wie kann man das richtig machen?
-
so gehts auf jeden fall.
#include <iostream> namespace bar { class B; } namespace foo { class A; } namespace foo { class A { public: friend class bar::B; protected: int value_from_a; }; } namespace bar { class B { public: void doit (void) { foo::A a; std::cout << a.value_from_a << std::endl; // << ERROR } }; } int main (void) { return 0; }
vielleicht kann jemand anderer erklären warum.
K.
-
(1) Der Prototyp von B muss im Namespace bar setehen. Wenn er in foo steht,
sucht er die Klasse selbst auch in foo.
(2) Es heißt friend class B;
(3) Der Prototyp von A ist nicht falsch, aber unnötig.