C++ 在for循环外部返回值
嘿,我正在尝试从列表中获取武器,并在我的玩家类中使用该武器。我认为我可以这样做,将所有武器放入一个列表,然后在任何时候我想要使用武器搜索列表,并将武器名称设置为搜索返回的值。但是我得到了一个运行时错误,因为我试图在for循环之外的列表中返回值。谁能给我一个解决这个问题的办法 这是我的密码: 武器等级C++ 在for循环外部返回值,c++,search,return,C++,Search,Return,嘿,我正在尝试从列表中获取武器,并在我的玩家类中使用该武器。我认为我可以这样做,将所有武器放入一个列表,然后在任何时候我想要使用武器搜索列表,并将武器名称设置为搜索返回的值。但是我得到了一个运行时错误,因为我试图在for循环之外的列表中返回值。谁能给我一个解决这个问题的办法 这是我的密码: 武器等级 DoublyLinkedList<Weapons> weaponsList; DoublyLinkedListIterator<Weapons> itr = weaponsL
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();
}
把它修好了,我愚蠢地把名字放在循环之外
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();
}
string武器::searchByWeaponID(int searchByID)
{
//循环遍历迭代器。
for(itr.Start();itr.Valid();itr.Forth())
{
//如果输入的对象与循环中的对象同名。
if(itr.Item().getWeaponID()==searchByID)
{
//从列表中打印出详细信息。
库特
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();
}