0

課題をコンパイルするためのコードを取得しようとして問題が発生しています。私は同様の問題 (まったく同じ割り当てのように見えるものでさえ) を見てきましたが、解決策はどれも機能しませんでした。

私のヘッダーファイル: 'complex.h':

#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED
#include <iostream>
#include <cstdlib>

using namespace std;

class Complex
{
public:
    // Consructors:
    Complex();
    Complex(double);
    Complex(double, double);

    // Operational Overloads:
    const bool operator == (const Complex&);
    const Complex operator + (const Complex&);
    const Complex operator + (const double&);
    const Complex operator - (const Complex&);
    const Complex operator - (const double&);
    const Complex operator * (const Complex&);
    const Complex operator * (const double&);
    friend ostream& operator << (ostream&, const Complex&);
    friend istream& operator >> (istream&, Complex&);

private:
    double real;
    double imaginary;
};

#endif // COMPLEX_H_INCLUDED

私の実装コード、「complex.cpp」:

#include "complex.h"
#include <iostream>
using namespace std;

/****************************************************************************
 *   Constructor ()                                                         *
 ****************************************************************************/
inline Complex::Complex(){
real = 0;
    imaginary = 0;
}

/****************************************************************************
 *   Constructor (double)                                                   *
 ****************************************************************************/
 inline Complex::Complex(double realPart)
             :real(realPart)
{
    imaginary = 0;
}

/****************************************************************************
 *   Constructor (double, double)                                           *
 ****************************************************************************/
inline Complex::Complex(double realPart, double imaginaryPart)
            :real(realPart), imaginary(imaginaryPart)
{}

/****************************************************************************
 *  Operator '==' -- Compares two Complex objects                           *
 *              Returns:                                                    *
 *                -- True if their real and imaginary values are equal      *
 *                -- False otherwise                                        *
 ****************************************************************************/
inline const bool Complex::operator == (const Complex& rhs)
{
    return ((this->real == rhs.real)  && (this->imaginary == rhs.imaginary));
}

/****************************************************************************
 *  Operator '+' -- Adds two Complex objects                                *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator + (const Complex& rhs)
{
    return (Complex((this->real + rhs.real), (this->imaginary + rhs.imaginary)));
}

/****************************************************************************
 *  Operator '+' -- Adds a real number to the Complex object                *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator+(const double& rhs)
{
    return Complex((this->real + rhs), this->imaginary);
}

/****************************************************************************
 *  Operator '-' -- Subtracts the number of another Complex object          *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator - (const Complex& rhs)
{
    return (Complex((this->real - rhs.real), (this->imaginary - rhs.imaginary)));
}

/****************************************************************************
 *  Operator '-' -- Subtracts a real number from the Complex object         *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator-(const double& rhs)
{
    return Complex((this->real - rhs), this->imaginary);
}

/****************************************************************************
 *  Operator '*' -- Multiplies together two Complex objects                 *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator * (const Complex& rhs)
{
    return (Complex((this->real * rhs.real) - (this->imaginary * rhs.imaginary), (this->real * rhs.imaginary) + (this->imaginary * rhs.real)));
}

/****************************************************************************
 *  Operator '*' -- Mutltiplies the Complex object by a real number         *
 *              Returns:                                                    *
 *                -- The new Complex object                                 *
 ****************************************************************************/
inline const Complex Complex::operator * (const double& rhs)
{
    return Complex((this->real * rhs), (this->imaginary * rhs));
}

/****************************************************************************
 *  Operator '<<' -- Outputs the Complex object in a comprehensive format   *
 *              Returns:                                                    *
 *                -- A reference to the ostream                             *
 ****************************************************************************/
ostream& operator <<(ostream& out, const Complex& comp)
{
    if(comp.real != 0)
    {
        out << comp.real;

        if(comp.imaginary > 0)
            out << " + ";
        else if (comp.imaginary < 0)
            out << " - ";
    }

    if(comp.imaginary != 0)
        out << comp.imaginary << "i";

    return out;
}

/****************************************************************************
 *  Operator '>>' -- Takes in a Complex object from the user                *
 *              Returns:                                                    *
 *                -- A reference to the istream                             *
 ****************************************************************************/
istream& operator >>(istream& in, Complex& comp)
{
    char sign; //Used to store input when looking for mathematical operators
    bool neg = false; //Stores whether or not any input values are <0

    // Checks for a negative value for the real
    sign = std::cin.peek();
    if(sign == '-'){
        neg = true;
        std::cin.ignore(1,'-');
    }

    in >> comp.real;

    //Negates the real value if necessary
    if(neg){
        comp.real = -comp.real;
        neg = false;
    }

    //Looks for the + or - operator, accounting for possible variations in whitespacing and hazardous input.
    do{
        in >> sign;
    }while (sign == ' ');

    if (sign == '-')
        neg = true;
    else if (sign != '+'){
        cout << "You did not properly format the input of a complex number. \nPlease use the format: 'a+bi' where 'a' is the real number, and 'b' is the imaginary number." << endl;
        cout << "The program is currently shutting down.";
        exit(EXIT_FAILURE);
    }

    sign = std::cin.peek();

    while(sign == ' ')
        in >> sign;

    in >> comp.imaginary;

    if(neg)
        comp.imaginary = -comp.imaginary;
}

そして、complex.cpp を実装する最終クラス:

 #include <iostream>
 #include "complex.cpp"

 using namespace std;

 int main()
 {
     Complex aComplex();

     cout << "Please enter a complex number, in the form of 'a+bi', where 'a' is a real number and 'b' the imaginary number." << endl;
     cin >> aComplex;

     cout << "You entered the value: " << aComplex << endl;

     return 0;
 }

コードをビルドしようとすると、次のエラーが表示されます。

.../Complex/main.cpp|12|error: ambiguous overload for ‘operator>>’ in ‘std::cin >> aComplex'`

誰にもアイデアはありますか?(私は Code::Blocks 12.11 を使用しており、Ubuntu 13.04 を使用しています。)

4

2 に答える 2

3

問題は、それaComplexが関数として宣言されていることです! おそらく、次のいずれかを書くつもりでした。

Complex aComplex;
Complex bComplex = Complex();
Complex cComplex{};

あなたaComplexが関数宣言である理由の説明を得るには、「最も厄介な解析」を探してください。

于 2013-09-22T00:13:23.337 に答える
0
Complex aComplex();

これは のコンストラクターを呼び出すのではなく、Complexゼロ引数を取り、タイプ のオブジェクトを返す関数の人々を作成しますComplex。オブジェクトをインスタンス化する場合は、空の括弧を削除する必要があります。

Complex aComplex;
于 2013-09-22T00:13:21.950 に答える