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

My getters are giving me a nonstandard and cannot convert error

我正在学习,但是在为角色的统计页面编写这些 getter 时,我收到一个错误,它是非标准的,它无法转换 const,它告诉我添加一个 & "to create a pointer to a member"

我试过用 * 使它们成为指针,我试过不使它们成为 const,使它们成为 public,添加我遗漏的任何 headers。

这些是许多行中唯一出现错误的行。它会产生大约 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;
};

只是更详细地解释错误,因为真正的答案已经是 ;对于同一问题,您将获得其中两个:

inline const double& getX() const { return this->getX; }
//                     ^                           ^ (!)

你有没有注意到这两个标识符是一样的?在您尝试 return getX 时,该函数已经声明并已知。而您现在正在尝试 return 正是这个功能。

Error C3867 'Player::getX': non-standard syntax; use '&' to create a pointer to member

使用成员函数,你可以做两件事:要么调用它们,要么获取它们的地址,形成一个成员函数指针。与 free-standing 函数和静态成员函数不同,non-static 成员函数不会自动转换为指针;对于这些,您需要明确指定 address-of 运算符 &:

void f() { }
class C { public: void f() { } };

void (*pf0)() = &f;        // explicitly taking address
void (*pf1)() = f;         // works for free standing functions
void (C::*pf0)() = &C::f  // ONLY can explicitly take address
//void (C::*pf1)() = C::f // gives you the error you saw already

嗯,函数指针的语法,尤其是成员 FP,真的很难看。通常您最好定义别名(typedefusing),或者在上述情况下,您可以使用 auto.

Error C2440 'return': cannot convert from 'const double &(__thiscall Player::* )(void) const' to 'const double &'

假设您已经修复了第一个错误(通过添加所需的 &),那么您声明的 return 类型与实际类型 return 之间存在类型不匹配]ing;前者是对 double 的引用,后者是成员函数指针。因此,您可以调整 return 值(在这种情况下不可能:您必须 return 指向函数的指针 returning 指向函数的指针 returning指向 ...) 或 select 已显示的正确成员的指针。

如果您想知道 __thiscall:它就是 calling convention,我们通常不必明确指定(除非我们需要 non-default——通常是例如,针对 WinAPI 进行编码的情况)。