假設我有兩個類。一個叫點:C++類訪問級別
class Point{
public:
Point(): x_(0), y_(0) {}
protected:
int x_, y_;
};
然後,我有另一個類,它派生點:
class Point3D : public Point {
public:
Point3D(): Point(), z_(0){}
double distance(Point3D pt, Point base) const;
protected:
int z_;
};
double Point3D::distance(Point3D pt, Point base) const
{
int dist_x, dist_y;
dist_x = base.x_ - x_;
dist_y = base.y_ - y_;
return sqrt(dist_x * dist_x +
dist_y * dist_y);
}
然後我得到了像錯誤:base.x_在此範圍內的保護。但是Point3D對Point的訪問級別是公共的,並且Point中的x_數據成員被保護。所以它應該是沒有錯誤的,對吧?有人可以幫我解決這個問題嗎?
順便說一句,你不應該使用pt代替這個,或者刪除參數pt? – deviantfan
@deviantfan哦忘記輸入參數pt。 –