我正在学习,但是在为字符的统计页面编写这些getter时,我得到一个错误,它是非标准的,它不能转换const,并且告诉我添加一个&“以创建指向成员的指针”
我试过用*来使它们成为指针,我试过不使它们成为const,而是使其成为公共对象,并添加我所缺少的任何标头。
这些是许多出现错误的唯一行。它产生大约30个错误。
inline const double& getX() const { return this->getX; }
inline const double& getY() const { return this->getY; }
inline const std::string& getName() const { return this->name; }
inline const int& getLevel() const { return this->level; }
inline const int& GetExpNext() const { return this->expNext; }
inline const int& getHP() const { return this->hp; }
inline const int& getStamina() const { return this->stamina; }
inline const int& getDamageMin() const { return this->getDamageMin; }
inline const int& getDamageMax() const { return this->getDamageMax; }
inline const int& getDefense() const { return this->getDefense; }
Run Code Online (Sandbox Code Playgroud)
这些是一些重复的错误。
Error C3867 'Player::getX': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const double &(__thiscall Player::* )(void) const' to 'const double &'
Error C3867 'Player::getY': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const double &(__thiscall Player::* )(void) const' to 'const double &'
Error C3867 'Player::getDamageMin': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const int &(__thiscall Player::* )(void) const' to 'const int &'
Error C3867 'Player::getDamageMax': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const int &(__thiscall Player::* )(void) const' to 'const int &'
Error C3867 'Player::getDefense': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const int &(__thiscall Player::* )(void) const' to 'const int &'
Error C3867 'Player::getX': non-standard syntax; use '&' to create a pointer to member
Error C2440 'return': cannot convert from 'const double &(__thiscall Player::* )(void) const' to 'const double &'
Error C3867 'Player::getY': non-standard syntax; use '&' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
很难确定,因为您只编写了带有错误的行,而不是发布了所有相关的代码。但是似乎您已经编写了这样的代码
class Player
{
public:
inline const double& getX() const { return this->getX; }
private:
double x;
};
Run Code Online (Sandbox Code Playgroud)
当你应该写这样的代码
class Player
{
public:
inline const double& getX() const { return this->x; }
private:
double x;
};
Run Code Online (Sandbox Code Playgroud)
注意x不是getX。
然后,正如已经指出了意见,inline,this和使用引用的是在这种情况下,所有多余的或坏的。所以你可以写更简单的
class Player
{
public:
double getX() const { return x; }
private:
double x;
};
Run Code Online (Sandbox Code Playgroud)