2

我有两行代码的问题。错误是:

|第 4 行|错误:在 '(' 标记| 之前需要构造函数、析构函数或类型转换

|第 50 行|错误: '!=' 标记之前的预期主表达式|

以下是代码位:

OPairType::OPairType (x=0, y=0);

return != (lh.x == rh.x && lh.y == rh.y);

如果您需要更多代码,请发表评论,我会提供。感谢您的任何帮助。

编辑:11/7/2013:这是标题代码:

class OPairType{
private:
 int x;
 int y;
public:
 OPairType (int=0, int=0);
 int getX() const;
 int getY() const;
 void setX(int);
 void setY(int);
 void setValues(int, int);
 friend OPairType operator + (OPairType, OPairType);
 friend OPairType operator - (OPairType, OPairType);
 friend bool operator == (OPairType, OPairType);
 friend bool operator != (OPairType, OPairType);
 friend std::ostream& operator << (std::ostream&, OPairType);

这是 .cpp 代码:

#include "OPairType.h"
#include <iostream>

OPairType::OPairType (int x, int y);

int OPairType::getX() const {
 return x;
}

int OPairType::getY() const {
 return y;
}

void OPairType::setX(int new_x) {
 x = new_x;
}

void OPairType::setY(int new_y) {
 y = new_y;
}

void OPairType::setValues (int new_x, int new_y){
 x = new_x;
 y = new_y;
}

OPairType operator + (OPairType lh, OPairType rh){
OPairType answer;

 answer.x = lh.x + rh.x;
 answer.y = lh.y + rh.y;

 return answer;
}

OPairType operator - (OPairType lh, OPairType rh){
OPairType answer;

 answer.x = lh.x - rh.x;
 answer.y = lh.y - rh.y;

 return answer;
}

bool operator == (OPairType lh, OPairType rh){
 return lh.x == rh.x && lh.y == rh.y;
}

bool operator != (OPairType lh, OPairType rh){
 return !(lh.x == rh.x && lh.y == rh.y);
}

std::ostream& operator << (std::ostream& out, OPairType c){
 out << "(" << c.x << ", " << c.y << ")";
 return out;
}
4

4 回答 4

3
return != (lh.x == rh.x && lh.y == rh.y);

那里的权利将被打破。默认情况下,该!=运算符是一个二元运算符,它需要数字或布尔类型作为它的操作数。该return语句不是布尔或数字类型。

也许你的意思是:

return !(lh.x == rh.x && lh.y == rh.y);

?

于 2013-11-06T20:28:31.607 回答
2

这不是!=这里的有效用法:

return != (lh.x == rh.x && lh.y == rh.y);

!=是二元运算符,需要两个操作数,C++ 标准草案5.10 Equality 运算符的语法如下:

equality-expression != relational-expression

并且像关系运算符(<、>、<=、>=)要求:

操作数应具有算术、枚举或指针类型,或类型 std::nullptr_t。

您的右操作数符合此要求,但作为语句的return不符合。看起来您想否定表达式,如果是这种情况,那么这就是您想要的:

return !(lh.x == rh.x && lh.y == rh.y);

更新

现在您更新了代码,我们可以在此处看到构造函数的定义:

OPairType::OPairType (int x, int y);

不完整,因为它没有主体,如下所示:

OPairType::OPairType (int X, int Y) : x(X), y(Y) {}
                                    ^            ^
                                    |            |
                                    |            Body
                                    Initialization list
于 2013-11-06T20:28:19.030 回答
1
OPairType::OPairType (int x=0, int y=0) // or another type
{
  // constructor's body here
}

or it could be

OPairType::OPairType (int x, int y); // constructor's declaration

return !(lh.x == rh.x && lh.y == rh.y); // test for unequality

编辑 2013/11/07

关于您的更改的一些说明。它看起来更好,但仍然存在一些错误。

  1. 你错过了构造函数的主体。我建议把它放在类声明中:

    OPairType (int xv = 0, int yv = 0) : x(xv), y(yv) { }
    

并在类外删除其声明。

  1. 你的操作'+','-'有一些问题。这是第一个:

    OPairType pair1(1, 2);
    OPairType pair2(3, 4);
    OPairType pair3(3, 4);
    
    (pair1 + pair2) = pair3;
    

这是胡说八道。为了防止这种用法,您必须返回 const 对象。第二个问题是没有运算符'+='和'-=',所以你不能写下面的代码:

    OPairType pair1(1, 2);
    OPairType pair2(3, 4);

    pair1 += pair2;
    // of course, you can do something like
    // pair1 = pair1 + pair2;
    // but it's not efficient

第三个问题非常小,至少对 64 位平台没有负面影响。您将对象按值传递给运算符。您的对象非常小,无法放入一个 64 位寄存器,您的编译器将进行某种优化以避免制作副本的开销。所以,这不是一个大问题。然而,通常更倾向于通过引用传递对象。

总结一下:

// define operator+= and operator-=
OPairType& operator+=(const OPairType& rhs)
{
  x += rhs.x;
  y += rhs.y;
  return *this;
}

// declare the returning type as const
friend const OPairType operator + (OPairType, OPairType);

// implement it through +=/-=
const OPairType operator + (OPairType lh, OPairType rh){
   return lh += rh;
}

// note that in case of references you would write the following
// implementation
// const OPairType operator + (const OPairType& lh, const OPairType& rh) {
//   OBPairType temp(lh); // make a copy
//   temp += rh; // add rh
//   return temp;  // return result
// }
于 2013-11-06T20:29:40.877 回答
0
OPairType::OPairType (x=0, y=0);

什么是 x 和 y 类型?你应该像这样将它添加到构造函数中。

OPairType::OPairType (int x, int y);
于 2013-11-06T20:29:39.283 回答