重载 + 运算符取一条线(两个坐标*2) + 一个点(两个坐标)

Overloading + Operator to take a line(Two coordinates*2) + a point(Two coordinates)

我有一个名为 OnePoint 的 class,它包含两个双精度值,即我的两个坐标 x 和 y。我有一条由两条 "OnePoint's" 组成的 class 线 - 因为两点是一条线。我还有一个 class 多边形,由 3 个 "OnePoint's" 组成,放入一个向量中 - 因为多边形有 3 个或更多点。

这是我的函数,它应该就是这样做的。

Polygon operator+(const OnePoint &lhs, const Line &rhs) {
    Polygon retvalu; 
    retvalu.inputvector(lhs); 
    retvalu.inputvector(rhs.getonevalue); //Line 150
    retvalu.inputvector(rhs.gettwovalue); //Line 151
    return retvalu;
} 

第 150 行和第 151 行的错误代码:

non-standard syntax; use '&' to create pointer to member 

我的所有代码:

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

class OnePoint {
private:
    double xvalue;
    double yvalue;

public:
    OnePoint(double x = 0.0, double y = 0.0) {
        xvalue = x;
        yvalue = y;
    }

    friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
        printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
        return printh;

    }

    void Plus(OnePoint a) {
        xvalue = xvalue + a.xvalue;
        yvalue = yvalue + a.yvalue;
    }

    void Minus(OnePoint b) {

        xvalue = xvalue + b.xvalue;
        yvalue = yvalue + b.yvalue;

    }

    OnePoint Plustwo(OnePoint a) {
        return (xvalue + a.xvalue, yvalue - a.yvalue);

    }

    void Change(double a, double b) {
        xvalue += a;
        yvalue += b;
    }

    void Print(OnePoint b) {

        cout << xvalue << "," << yvalue << endl;

    }

    /*OnePoint operator-(OnePoint a) {
        OnePoint temp;
        temp.xvalue = xvalue + a.xvalue;
        temp.yvalue = yvalue + a.yvalue;

        return temp;

        }


        friend OnePoint operator+(OnePoint a, OnePoint b) {
        OnePoint temp;
        temp.xvalue = a.xvalue + b.xvalue;
        temp.yvalue = a.yvalue + b.yvalue;

        return temp;

        }*/
};

class Line {
private:
    OnePoint onevalue;
    OnePoint twovalue;
public:
    Line(OnePoint a = OnePoint(), OnePoint b = OnePoint()) {
        onevalue = a;
        twovalue = b;

    }

    OnePoint getonevalue() {
        return onevalue;
    }

    OnePoint gettwovalue() {

        return twovalue;
    }

    friend ostream& operator<<(ostream& print, Line& cLine) {
        print << "{" << cLine.onevalue << ',' << cLine.twovalue << "}";
        return print;
    }

    /*friend Line operator+(OnePoint a, OnePoint b) {
        Line temp(a, b);
        return temp;
        }*/
};

class Polygon {
private:
    vector <OnePoint> polly;
public:
    Polygon(OnePoint a = OnePoint(), OnePoint b = OnePoint(), OnePoint c = OnePoint()) {
        polly.push_back(a);
        polly.push_back(b);
        polly.push_back(c);
    }

    friend ostream& operator<<(ostream& print, Polygon& pollyclass) {
        for(unsigned int i = 0; i < pollyclass.polly.size(); i++) {
            print << pollyclass.polly[i];
        }

        return print;
    }

    void outputvector() {

        for(unsigned int i = 0; i < polly.size(); i++) {
            cout << polly[i];
        }


    }

    void inputvector(OnePoint a) {
        polly.push_back(a);
    }
};

Line operator+(const OnePoint &lhs, const OnePoint &rhs){
    Line retval(lhs, rhs);
    return retval;
}

Polygon operator+(const OnePoint &lhs, const Line &rhs){
    Polygon retvalu;
    retvalu.inputvector(lhs);
    retvalu.inputvector(rhs.getonevalue); //Line 150
    retvalu.inputvector(rhs.gettwovalue); //Line 151

    return retvalu;
}

/*Polygon operator+(const Line &lhs, const OnePoint &rhs)
{
Polygon retval(rhs, lhs.getonevalue, lhs.gettwovalue);

return retval;
}*/

Polygon operator+(const Polygon &lhs, const Polygon &rhs) {
}

int main() {
    OnePoint a(3.0, 3.0);
    OnePoint b(1.0, 1.0);
    OnePoint y(3.0, 4.0);
    OnePoint i(4.0, 2.0);
    OnePoint p(3.0, 4.0);
    OnePoint m(4.0, 2.0);

    Line d(a, b);
    Polygon j(a, b, y);
    Polygon h(i, p, b);
    Polygon w;
    Line o;
    o = a + b;

    w = b + o;
    cout << w;
    cout << a << endl;
    cout << d << endl;
    cout << o << endl;
}

VS给我的错误很清楚:

error C3867: 'Line::getonevalue': function call missing argument list ...

getonevaluegettwovalue是函数,调用时需要加括号:

Polygon operator+(const OnePoint &lhs, const Line &rhs) {
    Polygon retvalu;
    retvalu.inputvector(lhs);
    retvalu.inputvector(rhs.getonevalue()); //Line 150
    retvalu.inputvector(rhs.gettwovalue()); //Line 151
    return retvalu;
}

现在,我们又遇到了一个错误。编译器会抱怨,因为这些函数不是 const 限定的,但您尝试在非 const 实例 rhs 上调用它们。他们不修改对象,只是 return 一个成员变量的副本。使它们符合常量条件:

OnePoint getonevalue() const {
    return onevalue; 
}

并且不要忘记实施 Polygon operator+(const Polygon&, const Polygon&)

此外,此代码要求 OnePoint 可以用一个参数构造,但这是错误的:

OnePoint Plustwo(OnePoint a) {
    return (xvalue + a.xvalue, yvalue - a.yvalue);
}

这使用逗号运算符,转换为:

OnePoint Plustwo(OnePoint a) {
    xvalue + a.xvalue;
    return yvalue - a.yvalue;
}

- 这需要从 doubleOnePoint 的隐式转换,这简直是一团糟。解决方案是 list initialization (C++11) 或标准结构:

OnePoint Plustwo(OnePoint a) {
    return {xvalue + a.xvalue, yvalue - a.yvalue}; // list initialization
}

OnePoint Plustwo(OnePoint a) {
    return OnePoint(xvalue + a.xvalue, yvalue - a.yvalue);
}

因此,Line 的构造函数必须使用 member initializer list:

Line(OnePoint a, OnePoint b) : onevalue(a), twovalue(b)
{
}

您拥有的是默认构造和赋值,而不是复制赋值。我想这就是我所能建议的,operator+ 需要 Polygon 默认构造函数,而你在 main 中默认构造 Line,所以你必须添加一个默认构造函数无论如何。