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

最佳答案

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

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


您是否注意到两个标识符相同?在尝试返回getX时,该函数已被声明并已知。现在,您正在尝试完全返回此函数。


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

With member functions, you can do two things: Either call them or get their address to form a member function pointer. Unlike free-standing functions and static member functions, non-static member functions are not converted automatically to pointers; for these, you need to explicitly specify the address-of operator &:

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


错误C2440'返回':无法从'const double&(__ thiscall Player :: *)(void)const'转换为'const double&'


假设您已经解决了第一个错误(通过添加必需的&),则在声明为返回类型的内容与实际返回的内容之间的类型不匹配;前者是对double的引用,后者是成员函数指针。因此,您可以调整返回值(在这种情况下是不可能的:您必须将指针返回到函数,将指针返回到函数,将指针返回到...),或选择正确的成员(如已显示)。

如果您对__thiscall感到疑惑,那就是calling convention,我们通常不必明确指定它(除非我们需要一个非默认值-例如,针对WinAPI进行编码通常就是这种情况)。

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

10-11 22:08
查看更多