首页 > 解决方案 > 我的吸气剂给了我一个非标准的并且无法转换错误

问题描述

我正在学习,但是在为角色的统计页面编写这些 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; }

这些是一些重复的错误。

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

标签: c++constantsgetter

解决方案


很难确定,因为您只发布了包含错误的行,而不是发布所有相关代码。但似乎你写过这样的代码

class Player
{
public:
    inline const double& getX() const { return this->getX; }
private:
    double x;
};

当您应该编写这样的代码时

class Player
{
public:
    inline const double& getX() const { return this->x; }
private:
    double x;
};

注意x不是getX

然后正如评论中已经指出的那样,在这种情况下inlinethis引用的使用和引用都是多余的或不好的。所以你可以写更简单的

class Player
{
public:
    double getX() const { return x; }
private:
    double x;
};

推荐阅读