gpt4 book ai didi

c++ - 没有成员可用于声明为类变量的对象

转载 作者:行者123 更新时间:2023-11-28 07:20:36 25 4
gpt4 key购买 nike

我很难解释这个问题,所以这里是我的代码的简单摘要:

假设我有一个名为“字符”的类

#include "myEnums"
#include "weapon"

character {
protected:
string characterName;
weapon* myWeapon;
public:
string getCharacterName();
void setCharacterName( string );
string getMyWeapon();
void setMyWeapon();
}

然后在“setMyWeapon”中使用这个简化的代码。

void character::setMyWeapon() {
this->myWeapon = new weapon("longsword");
//this->myWeapon = new weapon(myEnums::LONGSWORD); //Ideally this
}

string getMyWeapon() {
return this->myWeapon.tostring();
}

但是当我输入“.”时'myWeapon' 没有成员,有人知道怎么回事吗?假设“tostring”在“weapon.h”中定义...

最佳答案

由于 myWeapon 是一个指针,您需要解引用它以访问指针对象的成员:

myWeapon->tostring()
// ^^^^

关于c++ - 没有成员可用于声明为类变量的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19527696/

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