提问人:MrMorgotheron 提问时间:8/7/2022 更新时间:8/7/2022 访问量:52
非类类型的成员的错误请求 - 在复制的对象上使用方法时出现问题
Error request for member in which is of non class type - problem with using method on copied object
问:
我只是在学习C++。我想创建一个格斗文本游戏。一开始,我创造了战士。然后我让玩家选择一个战斗机,我创建一个新对象来存储有关所选战斗机的信息。这是我的代码:
fighter.h
#include <string>
using namespace std;
class Fighter
{
int charNumber;
string name;
int attack;
int def;
int hp;
int power;
int regeneration;
string background;
int choosingWarrior;
public:
Fighter(int charNumber = 99, string name = "Default" , int attack = 10, int def = 10, int hp = 10, int power = 10, int regeneration = 10, string background = "default");
~Fighter();
int showInfo() const;
void chooseCharacter();
};
fighter.cpp
#include "fighter.h"
#include <iostream>
#include <string>
Fighter::Fighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background) {
this->charNumber = charNumber;
this->name = name;
this->attack = attack;
this->def = def;
this->hp = hp;
this->power = power;
this->regeneration = regeneration;
this->background = background;
}
Fighter::~Fighter()
{
}
int Fighter::showInfo() const
{
cout << "Fighter number: " << charNumber << endl;
cout << "Fighter name: " << name << endl;
cout << "Statistics:" << endl;
cout << "Attack: " << attack << endl;
cout << "Defense: " << def << endl;
cout << "Health: " << hp << endl;
cout << "Power: " << power << endl;
cout << "Regeneration: " << regeneration << endl;
cout << "Background: " << background << endl;
}
main.cpp (main.cpp|39|error: 请求 'choosingFighter' 中的成员 'showInfo',其非类类型为 'Fighter(int, std::__cxx11::string, int, int, int, int, int, std:__cxx11::string)' {aka 'Fighter(int, std::__cxx11::basic_string, int, int, int, int, std::__cxx11::basic_string)'}| ||=== 构建失败:1 个错误、0 个警告(0 分钟、0 秒) ===|
#include <iostream>
#include <string>
#include "fighter.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
Fighter Scorpion(1, "Scorpion", 100, 100, 100, 100, 100, "Hanzo Hasashi, better known as Scorpion, is a resurrected ninja in the"
"Mortal Kombat fighting game series as well as the mascot of the games."
"He is one of the very few original characters debuting in the first"
"Mortal Kombat arcade game. He holds the distinction, along with"
"Raiden and Sub-Zero (in one form or another),"
"of appearing in every generation of Mortal Kombat games as a playable character." );
Fighter Shodan(2, "Shodan", 90, 110, 110, 90, 100, "S.H.O.D.A.N. (Sentient Hyper-Optimized Data Access Network),"
"later referred to as SHODAN"
"is an Artificial Intelligence and the main antagonist of the System Shock series."
"She is voiced by game writer and designer Terri Brosius." );
Fighter Legion(3, "Legion", 110, 90, 90, 110, 100, "Legion is the name taken by the gestalt consciousness formed by 1,183 geth"
"programs inhabiting a unique geth \"mobile platform\"." );
Fighter choosingFighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background);
Scorpion.showInfo();
Shodan.showInfo();
Legion.showInfo();
int choice;
cout << "Choose your character (write a number) 1. Scorpion; 2. Shodan; 3. Legion: " << endl;
cin >> choice;
do
{
if (choice == 1)
Fighter choosingFighter = Scorpion;
else if (choice == 2)
Fighter choosingFighter = Shodan;
else if (choice == 3)
Fighter choosingFighter = Legion;
else
cout << "Wrong number try again";
} while (choice != (1 || 2 || 3));
choosingFighter.showInfo();
return 0;
}
怎么了?在代码的这一部分中,我创建了一个新对象
Fighter choosingFighter(int charNumber, string name, int attack, int def, int hp, int power, int regeneration, string background);
在这里,我根据玩家选择的战斗机复制我的新对象
if (choice == 1)
Fighter choosingFighter = Scorpion;
else if (choice == 2)
Fighter choosingFighter = Shodan;
else if (choice == 3)
Fighter choosingFighter = Legion;
else
cout << "Wrong number try again";
} while (choice != (1 || 2 || 3));
在这里我使用我的方法,但我收到一个错误:
choosingFighter.showInfo();
(对非类类型的成员的错误请求)
我应该在代码中更改哪些内容?
答:
首先,声明为函数。然后,在语句中定义一组局部变量(在状态中,其生命周期以语句结尾)。choosingFighter
if
if
if
例如:
if (choice == 1)
Fighter choosingFighter = Scorpion;
与
if (choice == 1)
{
Fighter choosingFighter = Scorpion;
}
添加大括号应该可以更清楚地说明您在全新的作用域中定义了一个全新的变量。这也应该更清楚地表明,一旦区块以关闭结束,该变量将不复存在。}
现在是解决方案。
首先,我建议你定义为一个指针,这样我们就可以让它指向对象,而不是进行复制:choosingFighter
Fighter* choosingFighter = nullptr;
然后在语句中使其指向所选对象:if
if (choice == 1)
{
choosingFighter = &Scorpion;
}
最后,在使用指针之前检查指针是否有效:
if (choosingFighter)
{
choosingFighter->showInfo();
}
评论
下一个:C++ 中类的复制构造函数
评论
int Fighter::showInfo() const
不返回任何内容,因此程序具有未定义的行为。while (choice != (1 || 2 || 3))
不是你想的那样......基本上将评估为 1,然后与该单个值相比选择。在这里,人们通常会在最后一个子句中使用无限循环和指令......但是,如果您在这种情况下不循环,“再试一次”消息就没有多大意义了......1 || 2 || 3
break
else