简体   繁体   English

'!='标记之前的预期主要表达式

[英]expected primary-expression before '!=' token

I'm having a problem with two lines of code. 我遇到两行代码的问题。 The errors are: 错误是:

|line 4|error: expected constructor, destructor, or type conversion before '(' token| |第4行|错误:'('标记|之前的预期构造函数,析构函数或类型转换

|line 50|error: expected primary-expression before '!=' token| |第50行|错误:“!=”令牌之前的预期主表达式|

Here are the code bits: 以下是代码位:

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

and

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

If you need any more of the code, just leave a comment and I will supply it. 如果您需要更多代码,请发表评论,我会提供。 Thank you for any help. 感谢您的任何帮助。

Edit: 11/7/2013: Here is the header code: 编辑: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);

And here is the .cpp code: 这是.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);

That right there is going to break. 那个权利将要中断。 The != operator, by default, is a binary operator that expects either numeric or boolean types as it's operands. 默认情况下, !=运算符是一个二进制运算符,它期望数字或布尔类型作为其操作数。 the return statement is not a boolean or a numeric type. return语句不是布尔值或数字类型。

Perhaps you meant: 也许您的意思是:

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

?

This is not a valid use of != here: 这不是!=的有效用法:

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

!= is a binary operator and requires two operands, the grammar from the draft C++ standard section 5.10 Equality operators is as follows: !=是二进制运算符,需要两个操作数,C ++标准草案第5.10节“ 相等运算符”的语法如下:

equality-expression != relational-expression

and like the relational operators( <, >, <=, >= ) requires: 和关系运算符( <,>,<=,> = )一样要求:

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. 操作数应具有算术,枚举或指针类型,或类型为std :: nullptr_t。

Your right operand fits this requirement but return which is a statement does not. 您的正确操作数符合此要求,但返回语句不符合要求。 It looks like you want to negate the expression, if that is the case then this is what you want: 看起来您想否定表达式,如果是这种情况,那么这就是您想要的:

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

Update 更新

Now that you updated your code, we can see that this definition of your constructor here: 现在您已经更新了代码,我们可以在此处看到构造函数的以下定义:

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

is not complete because it does not have a body, something as follows would do: 不完整,因为它没有主体,可以执行以下操作:

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

EDIT 2013/11/07 编辑2013/11/07

A few notes regarding to your changes. 有关您的更改的一些注意事项。 It looks better but there're still some errors. 看起来更好,但仍然存在一些错误。

  1. You missed the constructor's body. 您错过了构造函数的主体。 I'd suggest to put it right into the class declaration: 我建议将其正确放入类声明中:

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

and remove its declaration outside the class. 并在类外删除其声明。

  1. Your operations '+', '-' have some problems. 您的操作“ +”,“-”有一些问题。 Here's the first one: 这是第一个:

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

that's a nonsense. 废话 To prevent such usage you have to return the const object. 为了防止这种用法,您必须返回const对象。 The second issue that there're no operators '+=' and '-=', so you cannot write the following code: 第二个问题是没有运算符'+ ='和'-=',因此您无法编写以下代码:

    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

The third issue is very minor and has no negative impact at least on 64bit platforms. 第三个问题很小,至少对64位平台没有负面影响。 You pass your objects to operators by value. 您可以按值将对象传递给运算符。 Your object is very small to fit in one 64bit register and your compiler will do some sort of optimization to avoid overhead with making a copy. 您的对象很小,无法容纳一个64位寄存器,并且您的编译器将进行某种优化,以避免制作副本的开销。 So, it's not a big problem. 因此,这不是一个大问题。 However it's usually more preferred to pass objects by reference. 但是,通常更优选通过引用传递对象。

Summarizing all that: 总结一下:

// 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);

what are x and y types? 什么是x和y类型? You should add it to constructor like that. 您应该像这样将其添加到构造函数中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM