簡體   English   中英

'!='標記之前的預期主要表達式

[英]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

有關您的更改的一些注意事項。 看起來更好,但仍然存在一些錯誤。

  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
// }
OPairType::OPairType (x=0, y=0);

什么是x和y類型? 您應該像這樣將其添加到構造函數中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM