2016-11-10 296 views
-2

下面是Point.h:错误C2228:左“.GetX”必须有类/结构/联合

class Point 
{ 
public: 
    Point(); 
    Point(int, int); 
    void SetX(int); 
    void SetY(int); 
    int GetX() const; 
    int GetY() const; 
private: 
    int x, y; 
}; 

在另一类‘雇员’,有一个方法,其参数是一个Point对象和我想调用它的成员方法GetX()和GetY(),但它失败,错误“C2228:left of'.GetX'必须有class/struct/union”和“C2228:'.GetY'左边必须有class /结构/联合“,为什么会发生这种情况?

Employee.h

class Employee 
{ 
public: 
    Employee(string str, Point &p) 
    { 
     name = str; 
     point = p; 
    } 
    void SetCoordinates(Point &p) 
    { 
     point.SetX(p.GetX()); //**error here** 
     point.SetY(p.GetY()); //**error here** 
    } 
private: 
    string name; 
    Point point; 
}; 
+0

凡为p定义? –

+0

因为“p”未定义。 –

+0

'str'也是未定义的。 –

回答

1

在您的实现,您使用的参数需要的名字,而不仅仅是类型:

class Employee 
{ 
public: 
    Employee(string str, Point p) 
    { 
     name = str; 
     point = p; 
    } 
    void SetCoordinates(Point &p) 
    { 
     point.SetX(p.GetX()); 
     point.SetY(p.GetY()); 
    } 
private: 
    string name; 
    Point point; 
};