gpt4 book ai didi

c++ - 我的 getter 给我一个非标准且无法转换的错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:55 26 4
gpt4 key购买 nike

我正在学习,但是在为角色的统计页面编写这些 getter 时,我收到一个错误,它是非标准的,它无法转换 const,它告诉我添加一个 & “创建指向成员的指针”

我试过用 * 使它们成为指针,我试过不使它们成为常量,使它们公开,添加任何我遗漏的 header 。

这些是唯一给出错误的行。它会产生大约 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

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

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

关于c++ - 我的 getter 给我一个非标准且无法转换的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com