【问题标题】:Overloaded preincrement operator error "more than one operator ++ matches these operands"重载预增量运算符错误“多个运算符 ++ 匹配这些操作数”
【发布时间】:2017-06-30 22:10:01
【问题描述】:

我在我的代码中重载了后增量 (a++) 运算符,我试图在我的代码中重载前增量运算符 (++a) 以执行一个使复数“a”平方的函数,但是,只有后增量运算符目前正在工作。我应该明确说明,前自增运算符和后自增运算符都调用相同的函数——它们都应该使用 square 方法/函数。因此,尽管处于职位/职位,他们应该给我相同的答案。

当被调用时,预增量运算符下面有一条红线,上面写着:“多个运算符 ++ 匹配这些操作数。但我已经明确地将“++()”和“++(int)”声明为考虑 ++a 和 a++。

我错过了什么吗?这是我的代码。

#include<iostream>
#include<iomanip>
using namespace std;

class ComplexNum
{
public:
    ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
    ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard
    ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
    ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
    ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
    ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators

    //overloaded operators
    ComplexNum& operator =  (const ComplexNum& that) = default;
    ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); }
    ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); }
    ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); }
    ComplexNum& operator ++() { return square(*this); } //called for ++num
    ComplexNum operator ++(int) { return square(*this); } //called for num++

    ostream& print(ostream& stm = cout) const;


private:
    float real; //float data member for real number (to be entered in by user)
    float imaginary; //float data member for imaginary number (to be entered in by user)

    //non-member overloaded operators
    //a is passed by value
    friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; }
    friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; }
    friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; }
    friend ComplexNum operator++(ComplexNum a) { return a++; } //friend for num ++
    friend ComplexNum operator++(ComplexNum a) { return ++a; } //friend for ++num

    friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); }
};

ComplexNum::ComplexNum(float a, float b)
{
    real = a;
    imaginary = b;
}

ComplexNum& ComplexNum::getComplexNum()
{
    ComplexNum keyboard;
    cout << "Enter real part of complex number: ";
    cin >> real;

    cout << "Enter imaginary part of complex number: ";
    cin >> imaginary;

    return keyboard; 
}

ComplexNum& ComplexNum::square(ComplexNum a)
{
    this->real = (a.real * a.real) - (a.imaginary * a.imaginary);
    this->imaginary = (2 * (a.real * a.imaginary));
    return *this;
}

ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b)
{
    this->real = a.real + b.real;
    this->imaginary = a.imaginary + b.imaginary;
    return *this;
}

ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b)
{
    this->real = a.real - b.real;
    this->imaginary = a.imaginary - b.imaginary;
    return *this;
}

ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b)
{
    this->real = (a.real * b.real) - (a.imaginary * b.imaginary);
    this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary);
    return *this;
}

ostream& ComplexNum::print(ostream& stm) const
{
    return stm << "(" << noshowpos << real << showpos << imaginary << "i)";
}

int main()
{
    ComplexNum a, b;
    cout << "First Complex Number:" << endl;
    a.getComplexNum();
    cout << endl;
    cout << "Second Complex Number:" << endl;
    b.getComplexNum();
    cout << endl;
    cout << fixed << setprecision(2)
        << "a == " << a << '\n'
        << "b == " << b << '\n'
        << "a+b == " << a + b << '\n'
        << "a-b == " << a - b << '\n'
        << "a*b == " << a*b << '\n'
        << "a*a == " << a*a << '\n'
        << "b*b == " << b*b << '\n';
        cout << "a*a (using postincrement) == " << a++ << '\n'; //works fine
        cout << "a*a (using preincrement) == " << ++a << '\n'; //gives me a [more than one operator error] underneath "++" 
        cout << endl;

    system("PAUSE");
}

【问题讨论】:

  • 可能是因为您有两个预增量运算符?它应该如何知道该调用哪一个?
  • 您的“//friend for num ++”和“//friend for ++num”具有完全相同的签名。所以其中一个在说谎。
  • @tkausl,我的印象是不同的回报隐含着不同的签名。我错了。谢谢你澄清这一点。这是否意味着我需要为 ++num 更改 //friend 内部的参数?再次感谢。
  • 尽管它们具有相同的返回类型。查看this list
  • 我检查了列表,我似乎正确地遵循了课程内的 post/pre 协议。我尝试将++numfriend 中的参数更改为operator++(ComplexNum&amp;, a),但它说a 未定义并给了我更多的红线。

标签: c++ operator-overloading pre-increment


【解决方案1】:

非会员预增量签名是

T& operator++(T& a); // ++a

非会员后增量是

T operator++(T& a, int); // a++

您必须将这些定义更改为

friend ComplexNum& operator++(ComplexNum& a, int) { return a++; } //friend for num ++
friend ComplexNum operator++(ComplexNum& a) { return ++a; } //friend for ++num

【讨论】:

  • 根据您的建议,我在课堂上修复了朋友符号。但是,我现在在主要内部的++aa++ 下遇到错误,再次说more than one operator "++" matches these operands。不过friendoverload下面的红线已经解决了。
  • 更具体地说,它给了我C2593: 'operator ++' is ambiguous
  • 所以我将两个朋友内部的ComplexNum&amp;a 更改为const ComplexNum&amp; a 并且它成功了,但现在我得到一个运行时错误,其中它需要复数a,平方它,然后平方再说一遍。什么时候应该给我同样的答案。 a++++a 应该是相同的答案,也就是说。
  • 哎呀..原来如此,抱歉。非成员运算符通常用于操作基本类型或已定义。您必须删除两种模式中的一种。
  • 所以我必须创建两个函数,一个用于后增量,一个用于预增量,以便在不相互渗透的情况下为两者获得相同的答案?
猜你喜欢
  • 2012-05-07
  • 2020-05-20
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多