Complex.cpp には有益な情報だけを入れています。
これが私の問題です。クラスを複雑にしました。これは、複雑に計算できることを意味します。演算子でcomplex + doubleを有効にしたいの+
ですが、complex + doubleではしか使えませんmain.cpp
。double 変数 + complex を使用すると、エラーが発生します。何故ですか?直せますか?
複雑な.h
#ifndef COMPLEX_H
#define COMPLEX_H
using namespace std;
class Complex
{
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
Complex operator*( const Complex & ) const; // mul
bool operator==( const Complex & ) const;
bool operator!=( const Complex & ) const;
friend ostream &operator<<( ostream & , const Complex& );
friend istream &operator>>( istream & , Complex& );
Complex operator+( const double & ) const;
//Complex &operator+( const double & ) const;
void print() const; // output
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex
#endif
コンプレックス.cpp
Complex Complex::operator+( const Complex &operand2 ) const
{
return Complex( real + operand2.real,imaginary + operand2.imaginary );
} // end function operator+
Complex Complex::operator+(const double &operand2) const
{
return Complex( real + operand2 , this->imaginary );
}
main.cpp
int main()
{
Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );
double ss = 5;
x = z + ss;
x = ss + z;//this syntax is illegal