Hi! I have a complex class like
If I now write
this generates C2666 in MSVC - overloads have similar conversions. Looks like ofs << 1 requires an implicit conversion and the compiler is unsure whether to use basic_ostream<_ Elem,_Traits>:: operator <<(int) or to convert int to Complex and use operator <<(std::ofstrea m &,const Complex &).
I could fix the problem in one of the following ways:
1. Remove operator<< from Complex.
2. Remove constructor Complex( const double &d ) from Complex.
3. Write ofs.basic_ostre am<char, char_traits<cha r>>::operator<< ( 1 ) instead of ofs << 1.
But nothing of the above seems acceptable. I can't believe things are really this screwed up, it doesn't seem like I'm doing anything wrong. Any suggestions?
Code:
class Complex
{
double re;
double im;
public:
Complex();
Complex( const double &r, const double &i );
Complex( const double &d );
friend ofstream &operator<<( ofstream &stream, const Complex &c );
[other stuff]
};
Code:
ofstream ofs( "file.txt" ); ofs << 1;
I could fix the problem in one of the following ways:
1. Remove operator<< from Complex.
2. Remove constructor Complex( const double &d ) from Complex.
3. Write ofs.basic_ostre am<char, char_traits<cha r>>::operator<< ( 1 ) instead of ofs << 1.
But nothing of the above seems acceptable. I can't believe things are really this screwed up, it doesn't seem like I'm doing anything wrong. Any suggestions?
Comment