當我嘗試Point通過將 x 和 y 成員變數保持為私有來通過參考傳遞物件時出現以下錯誤Point,這就是為什么我通過函式獲取 x 和 yGetX()以及GetY()
如何解決錯誤并使其按預期作業的原因到。
錯誤日志:
CppReview.cpp: In function 'int main()':
CppReview.cpp:92:30: error: cannot bind non-const lvalue reference of type 'Point&' to an rvalue of type 'Point'
92 | v.OffSetVector(v.GetStart(),1,3);
| ~~~~~~~~~~^~
CppReview.cpp:79:34: note: initializing argument 1 of 'void Vector::OffSetVector(Point&, int, int)'
79 | void Vector::OffSetVector(Point& p,int xoffset,int yoffset){
代碼 :
class Point{
private:
int x,y;
public:
Point(){
x = y = 0;
}
Point(int x,int y){
this->x = x;
this->y = y;
}
Point(float x,float y){
this->x = x;
this->y = y;
}
void SetX(int x){
this->x = x;
}
void SetY(int y){
this->y = y;
}
void Display(){
cout<<"("<<this->x<<','<<this->y<<")\n";
}
void move(int i=0,int j=0){
this->x =i;
this->y =j;
}
int& GetX(){
return (this->x);
}
int& GetY(){
return this->y;
}
};
class Vector{
Point start,end;
public:
Vector(){
this->start = Point(0,0);
this->end = Point(1,1);
}
Vector(int sx,int sy,int ex,int ey){
this->start = Point(sx,sy);
this->end = Point(ex,ey);
}
float ComputeDistance(Point,Point);
Point GetStart();
Point GetEnd();
void OffSetVector(Point&,int,int);
void Show();
};
float Vector::ComputeDistance(Point p1,Point p2){
int p1x = p1.GetX();
int p1y = p1.GetY();
int p2x = p2.GetX();
int p2y = p2.GetY();
float dist = sqrt((p1x-p2x)*(p1x-p2x) (p1y-p2y)*(p1y-p2y));
return dist;
}
Point Vector::GetStart(){
return this->start;
}
Point Vector::GetEnd(){
return this->end;
}
void Vector::OffSetVector(Point& p,int xoffset,int yoffset){
p.GetX() =xoffset;
p.GetY() =yoffset;
}
void Vector::Show(){
cout<<this->GetStart().GetX()<<','<<this->GetStart().GetY()<<" : "<<this->GetEnd().GetX()<<','<<this->GetEnd().GetY()<<"\n";
}
int main(){
Vector v(1,1,3,3);
v.Show();
v.OffSetVector(v.GetStart(),1,3);
return 0;
}
uj5u.com熱心網友回復:
函式 GetStart 回傳一個 Point 型別的臨時物件
Point GetStart();
而函式 OffsetVector 除外對物件的非常量參考。
void OffSetVector(Point&,int,int);
您不能將臨時物件與非 cinstant 左值參考系結。
更改函式 GetStart 的宣告,如
Point & GetStart();
此外,至少功能 GetEnd 應該更改為
Point & GetEnd();
您可以多載常量和非常量物件的函式
Point & GetSgtart();
cosnt Point & GetStart() const;
Point & GetEnd();
const Point & GetEnd() const;
uj5u.com熱心網友回復:
問題是運算式v.GetStart()是型別的右值Point,因此它不能系結到非 const 左值參考,例如Point&.
要解決這個問題,您需要更改成員函式p的第一個引數,如下所示: OffSetVector
class Vector{
//--------------------vvvvv------------------>const added here
void OffSetVector(const Point&,int,int);
};
//------------------------vvvvv------------------------------------>const added here
void Vector::OffSetVector(const Point& p,int xoffset,int yoffset){
//----------vv------------> = won't work here because of the added const
p.GetX() =xoffset;
//----------vv------------> = won't work here because of the added const
p.GetY() =yoffset;
}
但請注意,由于添加了const.
解決方案
因此,要解決該問題,您應該將回傳型別更改為GetStart,Point&如下所示:
class Vector{
//-------v----------------->return by reference
Point& GetStart();
};
//---v--------------------->return by reference
Point& Vector::GetStart(){
return this->start;
}
作業演示
同樣,您應該將GetEndas well的回傳型別更改為Point&而不是Point. 此外,還有一個用于多載GetStart和GetEndforconst和non-const物件的選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447537.html
