c++ - Accept multiple types using one stringstream function parameter -
i need implement own stream class error writing cout
. here im going create separate class , overload <<
operator accept basic data types. simple idea fallow. program not compiled.
error error c2679: binary '<<' : no operator found takes right-hand operand of type 'const wchar_t [20]' (or there no acceptable conversion)
#include <iostream> #include <string> #include <sstream> class errorwriter { public: std::wstringstream& errorwriter::operator<<(std::wstringstream& ss){ //write file //write console return ss; } }; int main(){ errorwriter eout; eout << l"this first error"; eout << l"\nthis second error" << 1 << 2.5 << true; }
so question
- how accept basic type argument using 1 function parameter (i not need write multiple operate overloader each data type).
- how other streams
cout
,stringstream
implement this wstringstream can constructed
wchar_t
std::wstringstream ss(l"this first error");
so why cannot convert wstringstream on fly (by works conversion constructor)
don't think there obvious way this.
the
basic_stringstream
constructor taking string declaredexplicit
- hence conversion doesn't automatically happen.
Comments
Post a Comment