gpt4 book ai didi

c++ - 解析问题c++

转载 作者:行者123 更新时间:2023-11-30 00:51:16 27 4
gpt4 key购买 nike

<分区>

我正在尝试从 Java 转换为 C++,当我想创建如下对象时遇到了这个问题:

Hero.cpp 文件:

#include "Hero.h"
#include <iostream>
#include <string>
#include "Enemy.h"

using namespace std;

Hero::Hero()
{
level = 1;
exp = 0;
life = 100*level;
dmg = 10*level;
}

void Hero::Attack(Enemy ene){
ene.loseHealth(dmg);
}

void Hero::Heal(){
life += 50;
}

void Hero::loseHealth(int x){
life -= x;
}

void Hero::receiveExp(){
exp += 10;
cout<<"Your hero is now level: "<< getLevel() << endl;

}

int Hero::getLevel(){
if(exp >= 10 && exp <= 19){
level = 2;
}else if(exp >= 20 && exp <= 29){
level = 3;
}else if(exp >= 30 && exp <= 39){
level = 4;
}else if(exp >= 40){
level = 5;
}else{
level = 1;
}
return level;
}
//Get all information about the hero
void Hero::printStatus(){
cout<<"Your level: " << level<<endl;
cout<<"Current exp: " << exp<<endl;
cout<<"Amount of HP: " << life << endl;
cout<<"DMG: " << dmg << endl;
}

问题是当我尝试编译时它说 Parsing issue to following:

void Hero::Attack(Enemy ene){
ene.loseHealth(dmg);
}

还有一个 Enemy 类,它看起来几乎一样,但在其 Attack 函数中存在相同的解析问题。

我正在使用 XCode5。它说:“未知类型名称‘敌人’”到这个:

void Attack(Enemy ene);

Hero.h 文件:

#ifndef __ConsoleGame__Hero__
#define __ConsoleGame__Hero__

#include <iostream>
#include "Enemy.h"
#include <cstdlib>

class Hero{
public:
Hero();
void Attack(Enemy ene);
void Heal();
void loseHealth(int x);
void receiveExp();
int getLevel();
void printStatus();

private:
int life;
int dmg;
int exp;
int level;

};

#endif /* defined(__ConsoleGame__Hero__) */

我的 Enemy.h 文件:

#ifndef __ConsoleGame__Enemy__
#define __ConsoleGame__Enemy__

#include <iostream>
#include "Hero.h"
#include <cstdlib>

class Enemy
{
public:
Enemy();
void Attack(Hero h);
void Heal();
void loseHealth(int x);
void setDmg(int x);
int getHealth();

private:
int life;
int dmg;
int level;

};

#endif /* defined(__ConsoleGame__Enemy__) */

我的 Enemy.cpp 文件:

#include "Enemy.h"
#include "Hero.h"
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

Enemy::Enemy()
{
life = 50 * (rand()% 6);
dmg = 5 * (rand()%6);
level = rand()% 6;
}

//Attack
void Enemy::Attack(Hero h){
h.loseHealth(dmg);
}

//Lose health
void Enemy::loseHealth(int x){
life -= x;
}

//Heal
void Enemy::Heal(){
life+= 10;
}

//Amount of hp
int Enemy::getHealth(){
return life;
}

//Set dmg
void Enemy::setDmg(int x){
dmg = x;
}

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