friend Operator darf nicht auf Elemente zugreifen
-
Servus,
ich weiss nicht obs an der sch**** Hitze liegt oder ob ich langsam verkalke, aber ich habe folgendes Problem:
Quaternion.hnamespace PMT { class Quaternion { public: friend std::ostream& operator << ( std::ostream &out, const Quaternion &x ); }; }
Quaternion.cpp
std::ostream& operator << ( std::ostream &out, const PMT::Quaternion& x ) { out << "( " << x.r << " + " << x.i << "i + " << x.j << "j + " << x.k << "k )"; return out; }
Fehler:
Quaternion.h: In function `std::ostream& operator<<(std::ostream&, PMT::Quaternion&)': Quaternion.h:13: error: `double PMT::Quaternion::r' is private Quaternion.cpp:104: error: within this context Quaternion.h:14: error: `double PMT::Quaternion::i' is private Quaternion.cpp:104: error: within this context Quaternion.h:15: error: `double PMT::Quaternion::j' is private Quaternion.cpp:104: error: within this context Quaternion.h:16: error: `double PMT::Quaternion::k' is private Quaternion.cpp:104: error: within this context
Was zuer Hoelle mach ich da falsch? Ich hab alles an Tutorials und Seiten abgegrast und die Suche konsultiert, aber mein Code entspricht exakt allen Beispielen. Wieso erkennt er nicht, dass die Funktion friend-Rechte hat?
Sieht jemand einen Fehler?
-
Ich glaube, das ist ein namespace Problem.
Bau um die Funktionsdefinition (also in Quaternion.cpp) mal ein "namespace PMT".
"friend" geht davon aus, daß es im selben Namensbereich definiert wird.
Daher unterscheiden sich bei dir Deklaration (mit friend) und Definition.
-
[cpp]
std::ostream& **PMT::**operator << ( std::ostream &out, const Quaternion& x )
{
out << "( " << x.r << " + " << x.i << "i + " << x.j << "j + " << x.k << "k )";
return out;
}[/cpp]
-
Himmelherrgottsakra.....
Dabei hab ich using namespace PMT doch drueber stehen, so ein Mist ein bloeder.... naja, jetzt gehts. Danke Jungs.