嘿,我正在尝试从列表中获取武器并在我的玩家类中使用该特定武器。我认为我可以做到这一点的方法是将所有武器放入列表中,然后在我想使用武器搜索列表并将武器名称设置为搜索返回的值时。但是我遇到了运行时错误,因为我试图在 for 循环之外返回列表中的值。谁能给我一个解决这个...
这是我的代码:
武器类
DoublyLinkedList<Weapons> weaponsList;
DoublyLinkedListIterator<Weapons> itr = weaponsList.getIterator();
Weapons :: Weapons()
{
this->weaponID = 0;
this->weaponName = "";
this->damage = 0;
this->weight = 0;
}
Weapons :: Weapons(int weaponID,string weaponName,int damage,int weight)
{
this->weaponID = weaponID;
this->weaponName = weaponName;
this->damage = damage;
this->weight = weight;
}
int Weapons :: getWeaponID()
{
return weaponID;
}
string Weapons :: getWeaponName()
{
return weaponName;
}
void Weapons::setWeaponName(string weaponName)
{
this->weaponName = weaponName;
}
int Weapons :: getDamage()
{
return damage;
}
int Weapons :: getWeight()
{
return weight;
}
void Weapons :: loadWeapons()
{
string fileName = "Weapons\\Weapons.txt";
ifstream infile(fileName);
string garbage;
int loadWeaponID;
string loadWeaponName;
int loadDamage;
int loadWeight;
while(infile >> garbage >> loadWeaponID >> garbage >> garbage
>> garbage >> loadWeaponName >> garbage >> loadDamage >> garbage
>> garbage >> loadWeight >> garbage)
{
cout << "Weapon ID: \t\t"<< loadWeaponID<< "\n";
cout << "Weapon Name: \t\t"<< loadWeaponName << "\n";
cout << "Damage: \t\t" << loadDamage <<"\n";
cout << "Weight: \t\t" << loadWeight << "\n";
Weapons w1 (loadWeaponID,loadWeaponName,loadDamage,loadWeight);
weaponsList.Append(w1);
}
}
void Weapons :: printWeapons()
{
int index = 0;
//Loop through the iterator.
for(itr.Start();itr.Valid();itr.Forth())
{
index++;
cout << "------------------Weapon------------------\n";
cout << "--------------------------------\n";
cout << "Position:\t\t" << index << "\n";
cout << "--------------------------------\n";
cout << "Weapon ID:\t\t" << itr.Item().getWeaponID() << "\n";
cout << "Weapon Name:\t\t" << itr.Item().getWeaponName() << "\n";
cout << "Damage:\t\t\t" << itr.Item().getDamage() << "\n";
cout << "Weight:\t\t\t" << itr.Item().getWeight() << "\n";
cout << "------------------------------------------\n";
}
cout << "Weapons: \t\t" << weaponsList.getCount() << "\n";
}
string Weapons :: searchByWeaponID(int searchByID)
{
//Loop through the Iterator.
for (itr.Start(); itr.Valid(); itr.Forth())
{
//If the object entered has the same first name as the one in the loop.
if (itr.Item().getWeaponID() == searchByID)
{
//Print out the details from the list.
cout << "------------------------------------------\n";
cout << "Experience:\t\t" << itr.Item().getWeaponID() << "\n";
cout << "First Name:\t\t" << itr.Item().getWeaponName()<< "\n";
cout << "Second Name:\t\t" << itr.Item().getDamage() << "\n";
cout << "Level:\t\t\t" << itr.Item().getWeight() << "\n";
cout << "------------------------------------------\n";
}
}
setWeaponName(itr.Item().getWeaponName());
cout << "Weapon NAme: " << getWeaponName();
return getWeaponName();
}
然后我想在播放器类中使用返回值:
//-------------------------------------------------------------------------------------------
// Name: Default Constructor.
//-------------------------------------------------------------------------------------------
Player :: Player()
{
this->firstName = "";
this->secondName = "";
this->level = 0;
this->experience = 0;
this->strength = 0;
string searchResult = weapons.searchByWeaponID(2);
this->weapons.setWeaponName(searchResult);
}