-1
안녕하세요 저는 목록에서 무기를 가져 와서 플레이어 클래스에서 해당 무기를 사용하려고합니다. 내가 할 수 있다고 생각하는 방법은 모든 무기를 목록에 넣는 것입니다. 그러면 언제든지 무기 검색을 사용하여 검색에서 반환 된 값으로 무기 이름을 설정하고 싶습니다. 그러나 for 루프 외부에있는 목록 내부의 값을 반환하려고하기 때문에 런타임 오류가 발생합니다.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);
}