我有一个名为OnePoint的类,它由两个double值组成,我的两个坐标是x和y。我有一个由两个“OnePoint”组成的班级线 - 因为两个点是一条线。我还有一个由3“OnePoint”组成的类Polygon放入矢量 - 作为Polygon有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;
}
答案 0 :(得分:1)
VS给我的错误很清楚:
error C3867: 'Line::getonevalue': function call missing argument list ...
getonevalue
和gettwovalue
是函数,因此您需要添加括号来调用它们:
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
上调用它们。它们不修改对象,只是将副本返回给成员变量。让它们符合const:
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;
}
- 这需要从double
隐式转换为OnePoint
,它只是搞砸了。解决方案是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)
{
}
您拥有的是默认构造和分配,而不是复制分配。我想这就是我可以建议的,Polygon
需要operator+
默认构造函数,而你在main中默认构造Line
,所以你必须添加一个默认构造函数反正。