Ik ben een applicatie aan het maken in C++. Ik heb een Time-class die een aantal operatoren overload (+, -, *, += , -= etc). Nu wil ik de gegevens in deze class ook graag serializen naar een std::wostringstream of andere streams. Nou heb ik daarvoor de volgende twee operatoren bedacht:
(de inhoud van de Time-class is te beschrijven in éen int). Nu zeurt de compiler echter al als ik simpelweg:
doe. De foutmeldingen die ik krijg zijn onder andere:
Hoe zorg ik er nou voor dat de compiler weet dat ik graag míjn implementatie van de << operator wil gebruiken? Ik heb al geprobeerd de operators in de std namespace te zetten, om ze andere signatures te geven (Time ipv const Time& enzo...) maar dat hielp allemaal niet.
C++:
1
2
3
4
5
6
7
8
9
| std::ostream& operator<<( std::ostream& strm, const Time& time ) { strm << (int)time; return strm; } std::istream& operator>>(std::istream& strm,const Time& time) { strm >> (int&)time; return strm; } |
(de inhoud van de Time-class is te beschrijven in éen int). Nu zeurt de compiler echter al als ik simpelweg:
C++:
1
2
3
| Time t(...); std::wostringstream wos; wos << t; |
doe. De foutmeldingen die ik krijg zijn onder andere:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Time' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,const char *)' [found using argument-dependent lookup]
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>
]
C:\Program Files\Microsoft Visual Studio 8\VC\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<wchar_t,std::char_traits<wchar_t>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>
]
c:\projecten\tjshow\tjshow\include\../../TJShow/lib/tinyxml.h(389): or 'std::ostream &operator <<(std::ostream &,const TiXmlNode &)'
c:\projecten\tjshow\tjshow\include\../../TJShow/lib/tinyxml.h(392): or 'std::string &operator <<(std::string &,const TiXmlNode &)'
c:\projecten\tjshow\tjshow\include\tjtime.h(154): or 'std::ostream &operator <<(std::ostream &,Time)' |
Hoe zorg ik er nou voor dat de compiler weet dat ik graag míjn implementatie van de << operator wil gebruiken? Ik heb al geprobeerd de operators in de std namespace te zetten, om ze andere signatures te geven (Time ipv const Time& enzo...) maar dat hielp allemaal niet.