gpt4 book ai didi

c++ - 我收到没有对象的错误 "Pokemon.cpp:11:18: error: cannot call member function ‘int Dice::roll()’”

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

口袋妖怪战斗模拟器

我正在构建一个简单的战斗模拟器。它将能够接受用户输入的 HP、攻击 lv 和防御 lv。

Pokemon::Pokemon() {
healthPoints = attackL = defL=0;
std::string Pname= "";
d20=Dice(20);
d6=Dice(6);

我有 4 个文件 Pokemon.h、Pokemon.cpp、Dice.h、Dice.cpp。口袋妖怪头文件包含

#include <iostream>
#include <string>
#include "Dice.h"


class Pokemon {

private:
int healthPoints, attackL, defL;
#include <iostream>
std::string Pname;
Dice d20;
Dice d6;

public:
Pokemon();
bool attack(Pokemon& opponent);
void userBuild();
} ;

Pokemon.cpp 使用 Pokemon 类来“构建”用户的 pokemon。我卡住的部分是在尝试使用

Pokemon::bool attack(){
d20=Dice.roll()

}

我收到错误“Pokemon.cpp:13:10: error: expected unqualified-id before ‘bool’

这是关于'bool attack'应该做什么的说明

bool attack(Pokemon& opponent)

use the d20 to roll random values for and attackBonus and a defenseBonus if the attackLevel+attackBonus of the attacker is greater than the the >>defenseLevel+defenseBonus of the defender, then roll for damage. Otherwise the attack misses if the attack lands, roll three d6's, add them up, and that is the damage display all attack information as reflected in the sample runs return true only if the opponent died because of the attack, otherwise returns false

最佳答案

实现成员函数的语法是

bool Pokemon::attack(Pokemon& opponent){
...
}

关于c++ - 我收到没有对象的错误 "Pokemon.cpp:11:18: error: cannot call member function ‘int Dice::roll()’”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21738038/

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