簡體   English   中英

復雜類中的重載運算符

[英]Overloaded operators in Complex class

我用重載運算符實現了復雜的類。 編譯后,它給了我一些錯誤,我不知道如何解決。這些錯誤通常與“ complex.cpp”無法識別Complex類型有關,即使“ complex.h”沒有錯誤。

complex.h

    /*Definition of Complex Class. This class contains overloading operators.*/

    #ifndef COMPLEX_H
    #define COMPLEX_H

    using std::ostream;
    using std::istream;

    class Complex{

        friend ostream &operator<<(ostream&, const Complex &);
        friend istream &operator>>(istream&, 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;//multiplication
        const Complex &operator=(const Complex &);//assignment
        bool const &operator==(const Complex &) const;//equivalent
        bool const &operator!=(const Complex &) const;//not equivalent
    private:
        double real;//real part
        double imaginary;//imaginary part
    }; 

    #endif

complex.cpp:

 //Definition of Member Functions of the Complex Class

    #include <iostream>

    using std::cout;
    using std::ostream;
    using std::istream;

    #include "complex.h"

    //Constructor
    Complex::Complex(double r, double i)
    :real(r), imaginary(i){};

    //Overloaded addition operator
    Complex Complex::operator+(const Complex &operand2) const
    {
        return Complex(real + operand2.real, imaginary +operand2.imaginary);

    };
    //Overloaded subtraction operator
    Complex Complex::operator-(const Complex &operand2) const
    {
        return Complex(real - operand2.real, imaginary - operand2.imaginary);

    };
    //Overloaded assignment operator
    const Complex& Complex::operator=(const Complex &right) 
    {
        real = right.real;
        imaginary = right.imaginary;
        return *this;
    };
    //Overloaded multiplication operator
    Complex Complex::operator*(const Complex &operand2) const{

        return((real *operand2.real)-(real*operand2.imaginary), (real*operand2.imaginary)-(imaginary*operand2.real));
    }

    bool Complex& Complex::operator==(const Complex &right) const{

        if ((real == right.real) && (imaginary == right.imaginary))
            return true;
        else
            return false;

    }
    bool Complex& Complex::operator!=(const Complex &right) const{

        if ((real != right.real) && (imaginary != right.imaginary))
            return true;
        else
            return false;

    }
    ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version

        output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
        return output;

    };
    istream &operator>>(istream&input, Complex &complex){//Get input from the user.

        input.ignore();//ignore '('
        input >> complex.real;
        input.ignore();//ignore ","
        input >> complex.imaginary;
        input.ignore();//ignore ')'
        return input;//it allows usage as cin>>a>>b>>c
    }

錯誤清單:

1-語法錯誤:標識符'Complex'62 1

2-錯誤C2065:'復雜':未聲明的標識符58 1

3-錯誤C2065:'復雜':未聲明的標識符65 1

4-錯誤C2065:'復雜':未聲明的標識符67 1

5-錯誤C2086:'布爾復合體':重新定義48 1

6-錯誤C2143:語法錯誤:'&'前缺少','56 1

7-錯誤C2143:語法錯誤:缺少';' '&'40 1 8-之前的錯誤C2143:語法錯誤:缺少';' 在“&”之前48 1

9-錯誤C2228:'.imaginary'的左側必須具有class / struct / union 58 1

10-錯誤C2228:'.imaginary'的左側必須具有class / struct / union 67 1

11-錯誤C2228:“。real”的左側必須具有class / struct / union 58 1

12-錯誤C2228:“。real”的左側必須具有class / struct / union 65 1

13-錯誤C2373:'Complex :: operator!=':重新定義; 不同類型的修飾符48 1

14-錯誤C2373:'Complex :: operator ==':重新定義; 不同類型的修飾符40 1

15-錯誤C2556:'int&Complex :: operator!=(const Complex&)const':重載函數僅在返回類型上與'const bool&Complex :: operator!=(const Complex&)const'不同48 1

16-錯誤C2556:'int&Complex :: operator ==(const Complex&)const':重載函數的區別僅在於返回類型與'const bool&Complex :: operator ==(const Complex&)const'40 1

17-錯誤C2805:二進制'運算符>>'參數太少62 1

18-錯誤C4430:假定缺少類型說明符-int。 注意:C ++不支持default-int 40 1

19錯誤C4430:缺少類型說明符-假定為int。 注意:C ++不支持default-int 48 1

20錯誤C4430:缺少類型說明符-假定為int。 注意:C ++不支持default-int 56 1

您可以在下面的所有代碼中看到:

#include <iostream>
using std::cout;
using std::ostream;
using std::istream;
using namespace std;

class Complex
{
    private:
        friend ostream &operator<<(ostream&, const Complex&);
        friend istream &operator>>(istream&, Complex&);
        double real;
        double imaginary;
    public:
        Complex(double = 0.0, double = 0.0);
        Complex operator+(const Complex&) const;
        Complex operator-(const Complex&) const;
        Complex operator*(const Complex&) const;
        const Complex &operator=(const Complex &);
        const bool operator==(const Complex&) const;
        const bool  operator!=(const Complex&) const;
};

Complex::Complex(double r, double i)
{
    this->real = r;
    this->imaginary = i;
}

Complex Complex::operator+(const Complex& operando2) const
{
    return Complex(real + operando2.real,imaginary + operando2.imaginary);
}

Complex Complex::operator-(const Complex& operando2) const
{
    return Complex(real - operando2.real,imaginary - operando2.imaginary);
}

const Complex& Complex::operator=(const Complex &right)
{
    real = right.real;
    imaginary = right.imaginary;
    return *this;
}

Complex Complex::operator*(const Complex &operando2) const
{
    return ((real * operando2.real) - (real * operando2.imaginary), (real * operando2.imaginary) - (imaginary*operando2.real));
}

const bool  Complex::operator==(const Complex &right) const
{
    if((real == right.real) && (imaginary == right.imaginary))
        return true;
    else
        return false;
}

const bool Complex::operator!=(const Complex &right) const
{
    if((real != right.real) || (imaginary != right.imaginary))
        return true;
    else
        return false;
}

ostream &operator<<(ostream&output, const Complex &complex) 
{
    output << '(' << complex.real << "," << complex.imaginary << ')';
    return output;
}

istream &operator>>(istream&input, Complex &complex)
{
    input.ignore();
    input >> complex.real;
    input.ignore();
    input >> complex.imaginary;
    input.ignore();
    return input;
}

main(void)
{
    Complex c1(4,5);
    Complex c2(8,9);
    Complex c3 = c1 + c2;
    Complex c4 = c2 - c3;
    Complex c5 = c1 * c3;
    Complex c6(4,5);
    bool varBool = c6 == c1;
    varBool = c6 != c2;

    c1 = c2 = c3;

    cout << c3 << endl;

    cin >> c3;
    cout << c3 << endl;
}

需要將Istream和Ostream運算符聲明為Complex類的朋友公共成員,以便他們訪問Complex類的私有成員。

friend ostream &operator<<(ostream&output, const Complex &complex){//Print as Complex object as (a,b) with overloaded version

    output << '(' << complex.real << "," << complex.imaginary << ')';//it allows usage as cout<<a<<b<<c
    return output;

};
friend istream &operator>>(istream&input, Complex &complex){//Get input from the user.

    input.ignore();//ignore '('
    input >> complex.real;
    input.ignore();//ignore ","
    input >> complex.imaginary;
    input.ignore();//ignore ')'
    return input;//it allows usage as cin>>a>>b>>c
}

另一方面,您要同時聲明兩種類型。 比較將返回布爾值,因此我刪除了Complex&:

bool Complex::operator==(const Complex &right) const{

    if ((real == right.real) && (imaginary == right.imaginary))
        return true;
    else
        return false;

}

我看到運算符!=中有一個錯誤,用&|替換了&& 因為不同的復數可以具有相同的實部或虛部。

bool Complex::operator!=(const Complex &right) const{

    if ((real != right.real) || (imaginary != right.imaginary))
        return true;
    else
        return false;

}

這就是我所看到的,嘗試告訴我們...

警告我不喜歡已解決...

Complex Complex::operator*(const Complex &operando2) const
{
    return **Complex**((real * operando2.real) - (real * operando2.imaginary), (real * operando2.imaginary) - (imaginary*operando2.real));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM