c++ - 我的吸气剂给了我一个非标准的并且无法转换错误
问题描述
我正在学习,但是在为角色的统计页面编写这些 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
解决方案
很难确定,因为您只发布了包含错误的行,而不是发布所有相关代码。但似乎你写过这样的代码
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
。
然后正如评论中已经指出的那样,在这种情况下inline
,this
引用的使用和引用都是多余的或不好的。所以你可以写更简单的
class Player
{
public:
double getX() const { return x; }
private:
double x;
};
推荐阅读
- android - 按下复选框以删除该卡片视图
- mysql - linux mint mysql-server 和 mysql-workbench 安装和设置问题
- python - 熊猫中的2x2差异表
- eclipse - 无法打开 Eclipse Marketplace Photon
- android - 服务中的前台通知在 Android 8.1 中不起作用
- c++ - std::unordered_map::insert 与 std::unordered_map::operator[]
- javascript - 使用 Websocket 对 Vue.js/Node.js 应用程序进行单元测试
- javascript - 在 contenteditable 中取消选择后范围文本相同
- c - C程序以数组的最大元素的索引输出数组的最大元素
- java - 如何优化我的 Lz77 滑动窗口压缩器?