gpt4 book ai didi

c++ - 计算成员值时 vector 的奇怪问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:34:49 24 4
gpt4 key购买 nike

这是我上一个问题的更新跟进:Fatal error from incrementing loop我已经完成了排序算法,它似乎工作正常。现在我的问题是尝试根据我的 vector 中的级别计算每个战士的健康状况(在 getHealth() 函数中)以正确计算。

斗士.h

#ifndef FIGHTER_H
#define FIGHTER_H

#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>

class Fighter
{
protected:
std::vector<Fighter> x;
std::string name;
int health, level;
//int damage;
public:
int getHealth(int);
void getEnemies(/*std::vector<Fighter> &*/);
void printFighter(/*std::vector<Fighter> &*/);
void sortFighters(/*std::vector<Fighter> &*/);
//friend std::istream & operator >> (std::istream & strm, Fighter & x);
//friend std::ostream & operator << (std::ostream & strm, const Fighter & f);
//void attack();
Fighter();
~Fighter();
};

class Player : public Fighter
{
private:
int experience;
public:
int getHealth(int);
void printFighter();
void getExperience(int);
void playerAttack();
Player();
~Player();
};

//class FightPub
//{
// private:
// Player player;
// Fighter enemy;
// public:
// //void fight();
// //void getStats();
//};
#endif

Fighter.cpp 这是 getHealth() 函数所在的位置:

//dynamically locate an array that holds the number of fighters, and for each fighter in the array, assign from the .txt 
//file the name and level from the fighter.
#include "Fighter.h"

#pragma region getEnemies
void Fighter::getEnemies(/*std::vector<Fighter> &baddie*/)
{
Fighter y;
std::ifstream inputFile;
inputFile.open("EnemyFighters.txt");
if(!inputFile)
{
std::cout << "error!" << std::endl;
}
else
{
while(!inputFile.eof())
{
std::string line;
inputFile >> line;
if (line == "<fighter>")
{
do
{
inputFile >> line;
y.name = line;
inputFile >> line;
y.level = atoi(line.c_str());
inputFile >> line;
y.health = getHealth(y.level);
x.push_back(y);
inputFile >> line;
}while(line != "</fighter>");
}
}
inputFile.close();
}
}
#pragma endregion

#pragma region getHealth

int Fighter::getHealth(int lv) //The main problem
{
if(lv >= 6)
{
//std::cout << "\nHealth Bonus!";
this->health = lv * 2;
std::cout << this->health << std::endl;
}
else if (lv > 1)
{
for (int i=1; i<lv; i++)
{this->health += 2;}
std::cout << this->health << std::endl;
}
return health;
}

#pragma endregion

#pragma region attack
//void Fighter::attack()
//{
// int randomAttack = rand() % 4 + 1;
//
// switch (randomAttack)
// case 1:
// {
// std::cout << "Enemy uses critical attack!"
// }
//}
#pragma endregion

#pragma region printFighter
void Fighter::printFighter(/*std::vector<Fighter> &baddie*/)
{
//std::cout << this;
for (int i=0; i<x.size(); i++)
{
std::cout << "\nName: " << x[i].name << std::endl
<< "Level: " << x[i].level << std::endl
<< "Health: " << x[i].health << std::endl;
}
}
#pragma endregion

void Fighter::sortFighters(/*std::vector<Fighter> &x*/)
{
Fighter * temp = new Fighter;
bool swap;

do
{
swap = false;
//std::cout << x.size() << std::endl;
for (int i=0; i/*+1*/<x.size()-1; i++)
{
std::cout << "The values being compared are: " << x[i].level << " and " << x[i+1].level << ".\n";
//if the level in the first is greater than the level in the next
if(x[i].level > x[i+1].level)//I get a fatal error here when it tries to compare
//the iterator with 1 that's outside its range
{
//assign the stats from the first to temp
temp->name = x[i].name;
temp->health = x[i].health;
temp->level = x[i].level;
//assign the stats from the next to the first
x[i].name = x[i+1].name;
x[i].health = x[i+1].health;
x[i].level = x[i+1].level;
//assign the ones in temp(the first) to the next
x[i+1].name = temp->name;
x[i+1].health = temp->health;
x[i+1].level = temp->level;
std::cout << "The values " << x[i].level << " and " << x[i+1].level << " have been exchanged.\n";
swap = true;
}

/*else if(x[i].level >= x[i+1].level)
{
temp->name = x[i].name;
temp->health = x[i].health;
temp->level = x[i].level;

x[i].name = x[i+1].name;
x[i].health = x[i+1].health;
x[i].level = x[i+1].level;

x[i+1].name = temp->name;
x[i+1].health = temp->health;
x[i+1].level = temp->level;
std::cout << "The values " << x[i].level << " and " << x[i+1].level << " have been exchanged.\n";
swap = true;
}*/

//else if (x[i].level < x[i+1].level)
//{
// /*temp->name = x[i].name;
// temp->health = x[i].health;
// temp->level = x[i].level;

// x[i].name = x[i+1].name;
// x[i].health = x[i+1].health;
// x[i].level = x[i+1].level;

// x[i+1].name = temp->name;
// x[i+1].health = temp->health;
// x[i+1].level = temp->level;*/
// std::cout << "The values " << x[i].level << " and " << x[i+1].level << " have not been swapped.\n";
// swap = false;
//}

//else if(x[i].level <= x[i+1].level)
//{
// /*temp->name = x[i].name;
// temp->health = x[i].health;
// temp->level = x[i].level;

// x[i].name = x[i+1].name;
// x[i].health = x[i+1].health;
// x[i].level = x[i+1].level;

// x[i+1].name = temp->name;
// x[i+1].health = temp->health;
// x[i+1].level = temp->level;*/
// std::cout << "The values " << x[i].level << " and " << x[i+1].level << " have not been swapped.\n";
// swap = false;
//}
}
}while (swap);

delete temp;
}
//std::istream & operator >>(std::istream & strm, Fighter x)
//{
// //x.name += strm.c_str();
// //x.level += atoi(strm.c_str());
// strm >> x.name;
// strm >> x.level;
// return strm;
//}

//std::ostream & operator << (std::ostream & strm, const Fighter f)
//{
// strm << "Name: " << f.name << std::endl;
// strm << "Level: " << f.level << std::endl;
// strm << "Health: " << f.health << std::endl;
// return strm;
//}
#pragma region Fighter C&D
Fighter::Fighter()
{
level = 1;
health = 10;
}
Fighter::~Fighter()
{
}
#pragma endregion
//void operator <()
//{
//}
//
//void operator >()
//{
//}
//
//void operator <=()
//{
//}
//
//void operator >=()
//{
//}
//
//
//
int Player::getHealth(int lv)
{
if(lv >= 6)
{
std::cout << "\nHealth Bonus!";
this->health = lv * 2;
}
/*else if (lv > 1)
for (int i = 1; i < lv; i++)
{this->health += 2;}*/
return health;
}

void Player::printFighter()
{
//std::cout << this;
std::cout << "\nPlayer's stats: \n"
<< "Level: " << this->level << std::endl
<< "Health: " << this->health << std::endl
<< "Experience: " << this->experience <<std::endl;
}

void Player::getExperience(int dmg)
{
experience += dmg;
if (experience >= (level * 10))
{
std::cout << "Congratulations, Player! You're up a level!\n";
level ++;
}
}

#pragma region Player C&D
Player::Player()
{
level = 1;
health = getHealth(level);
experience = 0;
}
Player::~Player()
{
}
#pragma endregion


//Player::printFighter()
//{
//
//}

斗士.txt

<fighter>
Tito
5
</fighter>

<fighter>
Cthulu
99
</fighter>

<fighter>
Gererweq
2
</fighter>

<fighter>
dnbq
3
</fighter>

<fighter>
hrbsrx
4
</fighter>

<fighter>
Flamer
4
</fighter>

<fighter>
fdhjje
1
</fighter>

<fighter>
hrbsrx
6
</fighter>

main.cpp

#include "Fighter.h"



int main()
{
unsigned seed = time(0);
srand(seed);

/*std::vector<Fighter> baddie;*/

Fighter * enemy = new Fighter;
Player * me = new Player;
enemy->getEnemies(/*baddie*/);
enemy->sortFighters(/*baddie*/);
enemy->printFighter(/*baddie*/);
me->printFighter();
delete enemy;
delete me;
return 0;
}

最佳答案

这里的 += 有问题。

 else if (lv > 1)
{
for (int i=1; i<lv; i++)
{this->health += 2;}
std::cout << this->health << std::endl;
}

每次调用 getHealth() 都会将计算出的健康状况存储到 this->health 变量中。当级别 >= 6 时,一切正常,但当级别较低时,一切都会崩溃。发生的事情是你读入:

<fighter>
Cthulu
99
</fighter>

并将健康设置为 198。然后您读入:

<fighter>
Gererweq
2
</fighter>

并将 2 添加到先前存储的值 198 - 因此您看到的是 200。

一个快速的创可贴修复方法是添加一行:

this->health = 10;

就在 for() 循环之上。

关于c++ - 计算成员值时 vector 的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586292/

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