错误 C2228:'.GetX'左侧必须具有类/结构/联合
error C2228: left of '.GetX' must have class/struct/union
下面是Point.h:
class Point
{
public:
Point();
Point(int, int);
void SetX(int);
void SetY(int);
int GetX() const;
int GetY() const;
private:
int x, y;
};
在另一个类"Employee"中,有一个方法,其参数是一个Point对象,我想调用其成员方法GetX()和GetY(),但它失败了,错误为"C2228: left of"。GetX'必须有class/struct/union '和' C2228: left of '。GetY"必须有类/结构/联合",为什么会发生这种情况?
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;
};
在您的实现中,您使用的参数需要名称,而不仅仅是类型:
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;
};
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 如何将结构/联合数组的成员传递到函数中
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- 为什么继承的结构成员在联合中无法访问?
- 在向量 C++14(无限制联合)的结构内的联合中创建和存储字符串
- 尝试初始化结构内的联合时出错
- 嵌套在联合中的结构嵌套在 C 中的结构中
- 成员引用基类型 'char' 不是 C++ 中的结构或联合
- 结构内部结构的联合 - 混合 C 和 C++
- 联合初始化中的结构
- 成员引用基类型不是结构或联合
- 在结构中使用联合的目的是什么?
- 错误 C2228 左侧'.setDay'必须具有类/结构/联合
- 如何从平面缓冲区中反序列化联合结构的 void* 值的大小
- 联合结构 C# - 等效或等于
- 将C++联合结构转换为 VB6
- C ++:联合 -> 结构,解释?
- 编译器特定联合结构实现的标准替代方案
- 具有构造函数的类的匿名联合/结构
- 联合结构有什么作用