C++ 设置与set functions参数同名的私有数据成员

C++ 设置与set functions参数同名的私有数据成员,c++,C++,如何正确设置这些私人成员: private: int x1,y1,x2,y2; 设置功能时,如下所示: void setPos(int x1, int y1, int x2, int y2) 我不能简单地将x1设置为x1,不幸的是,我被参数和变量名的这种实现所困扰 以下是完整的头文件: #ifndef SHIP_H #define SHIP_H class Ship { public: virtual ~Ship(void) {} virtual const char *

如何正确设置这些私人成员:

private:
int x1,y1,x2,y2;
设置功能时,如下所示:

void setPos(int x1, int y1, int x2, int y2)
我不能简单地将x1设置为x1,不幸的是,我被参数和变量名的这种实现所困扰

以下是完整的头文件:

#ifndef SHIP_H
#define SHIP_H
class Ship
{
  public:
    virtual ~Ship(void) {}
    virtual const char *name(void) const = 0;
    virtual int size(void) const = 0;
    int getX(int i) const;
    int getY(int i) const;
    void print(void) const;
    bool includes(int x, int y);
    int level(void) const;
    void decreaseLevel(void);
    static Ship *makeShip(char ch, int x1, int y1, int x2, int y2);
  protected:
    void setPos(int x1, int y1, int x2, int y2);
    int lev;
  private:
    bool checkConfig(int x1, int y1, int x2, int y2);
    int x1,y1,x2,y2;
};

class AircraftCarrier : public Ship
{
  public:
    AircraftCarrier(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};

class BattleShip: public Ship
{
  public:
    BattleShip(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};

class Cruiser: public Ship
{
  public:
    Cruiser(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};

class Destroyer: public Ship
{
  public:
    Destroyer(int x1, int y1, int x2, int y2);
    virtual const char *name(void) const;
    virtual int size(void) const;
};
#endif

使用
指针:

this->x1 = x1; // etc.
或使用类名进行限定:

MyClass::x1 = x1; // etc.

使用
指针:

this->x1 = x1; // etc.
或使用类名进行限定:

MyClass::x1 = x1; // etc.

有两种方法

void setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}

void setPos(int x1, int y1, int x2, int y2)
{
   Ship::x1 = x1; 
   Ship::y1 = y1; 
   Ship::x2 = x2; 
   Ship::y2 = y2;
}
如果函数是在类之外定义的,那么您必须编写例如

void Ship::setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}
AircraftCarrier( int x1, int y1, int x2, int y2 )
{
   setPos( x1, y1, x2, y2 );
}
编辑:对于您的代码,这些数据成员在相应的构造函数中按以下方式设置 比如说

void Ship::setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}
AircraftCarrier( int x1, int y1, int x2, int y2 )
{
   setPos( x1, y1, x2, y2 );
}

此外,还不清楚船名存储在何处。

有两种方法

void setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}

void setPos(int x1, int y1, int x2, int y2)
{
   Ship::x1 = x1; 
   Ship::y1 = y1; 
   Ship::x2 = x2; 
   Ship::y2 = y2;
}
如果函数是在类之外定义的,那么您必须编写例如

void Ship::setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}
AircraftCarrier( int x1, int y1, int x2, int y2 )
{
   setPos( x1, y1, x2, y2 );
}
编辑:对于您的代码,这些数据成员在相应的构造函数中按以下方式设置 比如说

void Ship::setPos(int x1, int y1, int x2, int y2)
{
   this->x1 = x1; 
   this->y1 = y1; 
   this->x2 = x2; 
   this->y2 = y2;
}
AircraftCarrier( int x1, int y1, int x2, int y2 )
{
   setPos( x1, y1, x2, y2 );
}

此外,还不清楚船名存储在何处。

另一种方法。更改参数的名称:

void setPos(int xa1, int ya1, int xa2, int ya2)
{
  x1 = xa1;
  y1 = ya1;
  x2 = xa2;
  y2 = ya2;
}

另一种方法。更改参数的名称:

void setPos(int xa1, int ya1, int xa2, int ya2)
{
  x1 = xa1;
  y1 = ya1;
  x2 = xa2;
  y2 = ya2;
}

你能列出你所有的限制吗?
setPos
是一个成员吗?我添加了头文件来澄清问题。为什么不给他们不同的名字呢?你是如何“坚持”这些名字的?您的意思是禁止您更改Ship.h中
setPos
声明中的参数名称,即使这不会影响对
setPos
的任何调用?显然,至少必须允许您在另一个文件中编辑
setPos
实现的某些部分——是否允许您像这样开始该实现,
void Ship::setPos(int new_x1,int new_y1,int new_x2,int new_y2)
?我无法更改Ship.h文件,尽管感谢您的输入,这个问题现在已经解决了!你能列出你所有的限制吗?
setPos
是一个成员吗?我添加了头文件来澄清问题。为什么不给他们不同的名字呢?你是如何“坚持”这些名字的?您的意思是禁止您更改Ship.h中
setPos
声明中的参数名称,即使这不会影响对
setPos
的任何调用?显然,至少必须允许您在另一个文件中编辑
setPos
实现的某些部分——是否允许您像这样开始该实现,
void Ship::setPos(int new_x1,int new_y1,int new_x2,int new_y2)
?我无法更改Ship.h文件,尽管感谢您的输入,这个问题现在已经解决了!我无法更改如上所述的名称我一直在使用此格式。我无法更改如上所述的名称我一直在使用此格式。我收到一个编译错误:错误:在非成员函数中无效使用“this”在哪个函数中?我收到一个编译错误:错误:在非成员函数中无效使用“this”哪个功能?