简体   繁体   中英

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|

|line 50|error: expected primary-expression before '!=' token|

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:

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:

#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.

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:

equality-expression != relational-expression

and like the relational operators( <, >, <=, >= ) requires:

The operands shall have arithmetic, enumeration, or pointer type, or type 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);

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

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. 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. 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. 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? You should add it to constructor like that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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