[英]expected primary-expression before '!=' token
我遇到两行代码的问题。 错误是:
|第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;
}
return != (lh.x == rh.x && lh.y == rh.y);
那个权利将要中断。 默认情况下, !=
运算符是一个二进制运算符,它期望数字或布尔类型作为其操作数。 return
语句不是布尔值或数字类型。
也许您的意思是:
return !(lh.x == rh.x && lh.y == rh.y);
?
这不是!=
的有效用法:
return != (lh.x == rh.x && lh.y == rh.y);
!=
是二进制运算符,需要两个操作数,C ++标准草案第5.10
节“ 相等运算符”的语法如下:
equality-expression != relational-expression
和关系运算符( <,>,<=,> = )一样要求:
操作数应具有算术,枚举或指针类型,或类型为std :: nullptr_t。
您的正确操作数符合此要求,但返回的语句不符合要求。 看起来您想否定表达式,如果是这种情况,那么这就是您想要的:
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
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
有关您的更改的一些注意事项。 看起来更好,但仍然存在一些错误。
您错过了构造函数的主体。 我建议将其正确放入类声明中:
OPairType (int xv = 0, int yv = 0) : x(xv), y(yv) { }
并在类外删除其声明。
您的操作“ +”,“-”有一些问题。 这是第一个:
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
// }
OPairType::OPairType (x=0, y=0);
什么是x和y类型? 您应该像这样将其添加到构造函数中。
OPairType::OPairType (int x, int y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.