Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
template<class T> class bla { ... } ... bla<int> t; cout << t << endl; |
Prinzipiell solltest du den operator innerhalb deiner Template-Klasse definieren, dann ist auch der Typ bekannt.
ich habe versucht den stream-Operator für meine Klasse zu überladen (was logischerweise nicht das gewünschte Resultat brachte) und ich habe veruscht den Operator Global für ostream und meine Klasse zu überladen wobei er dabei allerdings eine Templateargumentliste will (die ich ihm nicht mit geben kann da ich ja nicht weis welcher Typ es ist).
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
template<class T> class BaseType { private: T m; public: string m_name;public: BaseType(){m_name = "BaseType";}; public: BaseType(const T& o){this->m = o;} public: BaseType& operator= (const T& o){this->m = o; return BaseType<T>(this->m);} public: BaseType& operator= (const BaseType& o){this->m = o.m; return BaseType<T>(this->m);} public: BaseType& operator< (const T& o){return this->m < o;} public: BaseType& operator< (const BaseType& o){return this->m < o.m;} public: BaseType& operator> (const T& o){return this->m > o;} public: BaseType& operator> (const BaseType& o){return this->m > o.m;} public: bool operator==(const T& o){return this->m == m;} public: bool operator==(const BaseType& o){return this->m == o.m;} public: BaseType& operator<= (const T& o){return (this->m < o || this == o);} public: BaseType& operator<= (const BaseType& o){return (this->m < o.m || this == o);} public: BaseType& operator>= (const T& o){return (this->m > o || this == o);} public: BaseType& operator>= (const BaseType& o){return (this->m > o.m || this == o);} //friend ostream& operator<<(ostream& s, BaseType& t); public: string TypeAsText(){return this->m_name;}public: string ValueAsText() { stringstream s;s << this->m;return s.str(); } }; // //global functions for BaseType // //template<typename T> //ostream& operator<<(ostream& s, BaseType<T>& t) //{ // return s << t.m; //} |
C-/C++-Quelltext |
|
1 2 3 4 5 |
public: ostream& operator << (BaseType& o) { ostream s; s<<o;return s; } |
C-/C++-Quelltext |
|
1 2 3 |
BaseType<int> i; BaseType<float> f; if(f == i){...} |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Zeus« (24.08.2010, 19:13)
In deiner Implementierung sind gleich zwei Fehler.
C-/C++-Quelltext
1 public: ostream& operator << (BaseType& o)
C-/C++-Quelltext |
|
1 |
ostream& operator << (ostream out&, BaseType& o) |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
template<class T> class foo { public: foo ():x (T()) {} template<class T> friend std::ostream& operator << ( std::ostream& o, const foo<T> f ) { std::cout << f.x << std::endl; return o; } private: T x; }; int main() { foo<int> f; std::cout << f; } |
Nur sind für mich >= und <= in einem Punkt identisch ... also kann ich nicht sagen dass >= immer dann eintrifft wenn nicht <= ...
Quellcode |
|
1 2 |
a >= b wenn gilt NOT(a<=b) OR (a = b) a > b wenn gilt NOT(a<=b) |
Werbeanzeige