在for循环之外返回一个值

时间:2013-05-06 14:38:01

标签: c++ search return

嘿我正试图从列表中获取武器并在我的玩家类中使用该特定武器。我认为我可以这样做的方法是将所有武器放入一个列表,然后随时我想使用武器搜索列表并将武器名称设置为搜索返回的值。但我得到一个运行时错误,因为我试图返回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);
    }

1 个答案:

答案 0 :(得分:1)

修复它我愚蠢地将名称设置在循环之外。

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();
}