<<
コードでフレンド関数としてオーバーロードしました。カスケード中にブレースを使用した後でも、まだエラーが発生します
error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits =
std::char_traits<char>]((* & std::cout), ((const char*)"complex conjugate is "))
<< c.myComplex::operator~()’
クラスがありますmyComplex
class myComplex
{
private:
float real;
float imaginary;
public:
myComplex();
myComplex(float a,float b);
friend std::ostream& operator<<(std::ostream& iso,myComplex& a);
myComplex operator~() const;
};
のオーバーロード<<
と~
コンストラクタは次のとおりです。
std::ostream& operator<<(std::ostream& oso,myComplex& a)
{
oso<<a.real<<" + "<<a.imaginary<<"i"<<std::endl;
return oso;
}
myComplex myComplex::operator~() const
{
return myComplex(this->real,-1*this->imaginary);
}
myComplex::myComplex(float a,float b)
{
real = a;
imaginary = b;
}
以下のようにメインで使っていますが、
Line 1: std::cout << "c is " << c << "\n";
Line 2: std::cout << "a is " << a << "\n";
Line 3: ((std::cout <<"complex conjugate is ")<<(~c))<<"\n";
Line 3: std::cout <<"complex conjugate is "<<~c<<"\n";
1 行目と 2 行目は問題なく動作しますが、3 行目でエラーが発生します。行 1 と 2 にカスケードでき、それ以降~c
も返す場合、ostream
なぜエラーが発生するのですか?