Zuweisungsoperator und Namespace?
-
Hallo,
ich hab' ne' Frage zum Zuweisungsoperator. Und zwar habe ich ein Bsp. wie folgt:
class MyClass_t { public: MyClass_t(); public: MyClass_t & operator=(const MyClass_t &); public: ... }; MyClass_t::MyClass_t() { } MyClass_t & MyClass_t::operator=(const MyClass_t & rhs) { if (this == &rhs) return *this; *itsValue = rhs.itsValue(); return *this; } MyClass_t::...Funktioniert ohne Probleme.
Doch ändere ich das in
namespace MyNamespace{ class MyClass_t { public: MyClass_t(); public: MyClass_t & operator=(const MyClass_t &); public: ... }; } MyNamespace:: MyClass_t::MyClass_t() { } MyNamespace:: MyClass_t & MyClass_t::operator=(const MyClass_t & rhs) { if (this == &rhs) return *this; *itsValue = rhs.itsValue(); return *this; } MyNamespace:: MyClass_t::......geht nix mehr.
Der Gnu Compiler sagt:
error: syntax error before `::' token
...und bezieht sich auf die Zeile 22 im zweiten Beispiels dieses Postings.
Watt' is' hier krumm?
Besten Dank
-
die implementierung wird auch in namespace MyNamespace{} geklammert, nicht mit MyNamespace:: davor.
-
Alternativ könntest du auch vor alle MyClass_t den Namensbereich setzen, z.b.
MyNamespace::MyClass_t & MyNamespace::MyClass_t::operator=(const MyNamespace::MyClass_t & rhs) { }Aber dies ist mehr Schreibarbeit und unübersichtlicher, daher ist die Vorgehensweise, welche @thordk genannt hat, am besten...