简体   繁体   English

C++ 重载 + 运算符不工作

[英]C++ overloading + operator not working

Hello guys look to my code I'm trying to make a program which asks you to enter the first value by grams And the second value is kilograms and then convert kilograms to grams by an overloaded + operator but it doesn't work why大家好,看看我的代码,我正在尝试制作一个程序,要求您按克输入第一个值,第二个值是千克,然后通过重载 + 运算符将千克转换为克,但它不起作用为什么

#include <iostream>
using namespace std;
class ADD{
private:
    int Fval;
    int Sval;
public:
    ADD(){
            cout << "WELCOME TO OUR PROGRAM"<<endl<<"PLEASE ENTER THE FIRST VALUE BY GRAMS :";
            cin >> Fval;
            cout << "PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :"; cin >> Sval;
        }
    ADD operator+(ADD& add){
        add.Sval *= 1000;
        return add;
    }
    int plus(){
        return Fval+Sval;
    }
};
int main(){
    ADD a1;
    cout << "THE TOTAL VALUE = " << a1.plus() << " GRAMS";
}

No Effect look to the output没效果看output

WELCOME TO OUR PROGRAM
PLEASE ENTER THE FIRST VALUE BY GRAMS :2
PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :3
THE TOTAL VALUE = 5 GRAMS

That means the + operator doesn't multiply 3 by 1000 Why??这意味着 + 运算符不会将 3 乘以 1000 为什么?

That's because you're not calling operator + .那是因为你没有调用operator +

You're calling ADD::plus() :您正在调用ADD::plus()

int plus(){
    return Fval+Sval;
}

Fval and Sval are integers, which you're adding up. FvalSval是整数,您将它们相加。 It's as simple as that.就这么简单。

EDIT:编辑:

Your code is fishy.你的代码很可疑。

ADD operator+(ADD& add){
    add.Sval *= 1000;
    return add;
}

Multiplication inside operator + ? operator + ? Really?真的吗? Also, not that you're modifying the parameter, which you shouldn't.另外,并不是说您正在修改参数,您不应该这样做。 It's not intuitive.这不直观。 If you really must do this:如果你真的必须这样做:

ADD operator+(const ADD& add){
    ADD ret;
    ret.Sval = add.Sval * 1000;
    return ret;
}

The way operator overloading works is that if you have an object of type X, like so:运算符重载的工作方式是,如果你有一个 X 类型的 object,就像这样:

class X {
    public:
        X( int v ) : x( v ) {}
        int value();
        X operator +( const X& y ) {
            return X( value() + y.value() );
        }
    private:
        int y;
    };

Now if you declare two objects of type X , say X ex and X wye , you can say现在,如果您声明两个X类型的对象,比如X exX wye ,您可以说

X zed = ex + wye;

and get the right result.并得到正确的结果。 If type X had more than just a single int field the effect would be more interesting.如果类型X不止有一个int字段,效果会更有趣。 For example, you could implement 2D vectors and points, and then operations that add and subtract vectors, to get vectors, add and subtract vectors to/from points to get points, and subtract points to get vectors.例如,您可以实现 2D 向量和点,然后执行加减向量的操作以获取向量,向点加减向量以获取点,以及减去点以获取向量。

Hopefully this will give you enough of an idea of what's going on to be able to restructure your code to get it to do what you want.希望这会让您对正在发生的事情有足够的了解,以便能够重组您的代码以使其执行您想要的操作。 I may have a detail of syntax wrong, as I'm typing as I go.我可能有语法错误的细节,因为我输入的是 go。

Also, I often find the right thing is to declare a friend operator when I want a binary operator:此外,我经常发现正确的做法是在需要二元运算符时声明友元运算符:

friend operator + ( vector2D a, vector2D b );

... ...

inline operator + ( vector2D a, vector 2D b ) {
    return vector2D ( a.x + b.x, a.y + b.y );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM