以下向量加法运算有什么错误,如何使它正确运行?

时间:2019-09-20 20:12:57

标签: c++ vector addition

#include <iostream>
using namespace std;

class Vektor {
    public:
    int x, y;

    Vektor(int a, int b) {
        x = a;
        y = b;
    }

};

Vektor& Vektoraddition(Vektor& vec1, Vektor& vec2) {
    Vektor Vektorres(0,0);
    Vektorres.x = vec1.x + vec2.x;
    cout << Vektorres.x << endl;
    Vektorres.y = vec1.y + vec2.y;
    cout << Vektorres.y << endl;
    return Vektorres;
}

int main() {
    Vektor Vektor1(2, 3);
    Vektor Vektor2(3, 3);
    Vektor& Vecresult = Vektoraddition(Vektor1, Vektor2);
    cout << Vecresult.x << ", " << Vecresult.y << endl;return 0;
}

当我逐步进行调试时,“ Vecresult.x”和“ Vecresult.y”的结果是有效的,但是,当我想将其打印为“ Vecresult.y”的结果时,它将以某种方式变为随机数。希望有人可以帮助我...

0 个答案:

没有答案