3

我在让基于矢量的库存系统工作时遇到问题。我能够列出库存中的物品,但不能允许访问用户选择的物品。这是代码:

struct aItem
{
    string  itemName;
    int     damage;

    bool operator==(aItem other)
    {
        if (itemName == other.itemName)
            return true;
        else
            return false;
    }
};

int main()
{
    int selection = 0;


    aItem healingPotion;
    healingPotion.itemName = "Healing Potion";
    healingPotion.damage= 6;

    aItem fireballPotion;
    fireballPotion.itemName = "Potion of Fiery Balls";
    fireballPotion.damage = -2;

    aItem testPotion;
    testPotion.itemName = "I R NOT HERE";
    testPotion.damage = 9001;
    int choice = 0;
    vector<aItem> inventory;
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(fireballPotion);

    cout << "This is a test game to use inventory items. Woo!" << endl;
    cout << "You're an injured fighter in a fight- real original, I know." << endl;
    cout << "1) Use an Item. 2) ...USE AN ITEM." << endl;

switch (selection)
    {
    case 1:
        cout << "Which item would you like to use?" << endl;
        int a = 1;
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {

            cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

        cin >> choice;

^^^^ 这条线以上的一切,都有效。我认为我的问题是 if 语句,但我无法弄清楚我的语法哪里出错了,或者是否有更好的方法来做我正在做的事情。

        if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
            cout << "You used a healing potion!";
        else
            cout << "FIERY BALLS OF JOY!";
        break;

    case 2:
        cout << "Such a jerk, you are." << endl;
            break;

    }

编辑:我认为我没有正确表示这一点。我需要让玩家的选择影响显示的消息。这是第一个片段的示例输出:

Item 1: Healing Potion
Item 2: Healing Potion
Item 3: Healing Potion
Item 4: Potion of Fiery Balls

MAKE YOUR CHOICE. 
Choice: 

从那里,玩家可以输入 1-4,我想要的是将数字(减 1,以反映从零开始的向量)传递给 find,然后它将确定(在这个小例子中)是否库存中的物品[选择 - 1] 是一种治疗药水。如果是这样,显示“你使用了治疗药水!” 如果不是,则显示“快乐的火球”。

4

3 回答 3

2

三个问题。

一,您的运营商应声明为:

bool operator==(const aItem& other) const

二,在这段代码中:

find(inventory.begin(), inventory.at(choice), healingPotion) != inventory.end())

您不是从begin()to搜索整个向量end()- 您只是在搜索 from begin()to at(choice)whereat(choice)指向您的搜索集的最后一个位置。所以你要么应该这样做:

find(&inventory.at(0), &inventory.at(choice), healingPotion) != &inventory.at(choice))

或这个...

find(inventory.begin(), inventory.end(), healingPotion.itemName) != inventory.end())

编辑三,您正在尝试将苹果与橙子进行比较。您正在搜索对象以找到匹配的vector对象,但您发送到的参数不是对象,它是数据成员之一。aItemaItemfindaItemaItem

您应该搜索匹配的项目,如下所示:

find( inventory.begin(), inventory.end(), healingPotion ) != inventory.end() )
                                            ^^^^^^^^

在 C++03 中,您可以提供一个仿函数:

#include <functional>
struct match_name : public std::unary_function<aItem, bool>
{
    match_name(const string& test) : test_(test) {}
    bool operator()(const aItem& rhs) const
    {
        return rhs.itemName == test_;
    }
private:
  std::string test_;
};

...然后使用搜索匹配find_if

find_if( inventory.begin(), inventory.end(), match_name(healingPotion.itemName) ) // ...

在 C++11 中,您可以使用闭包来简化这种混乱:

string test = healingPotion.itemName;
if( find_if( inventory.begin(), inventory.end(), [&test](const aItem& rhs)
{
    return test == rhs.itemName;
}) == inventory.end() )
{
  // not found
}
于 2012-11-12T19:02:21.443 回答
0

代替:

case 1:
        cout << "Which item would you like to use?" << endl;
        int a = 1;
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {

            cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

        cin >> choice;
        if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
            cout << "You used a healing potion!";
        else
            cout << "FIERY BALLS OF JOY!";
        break;

    case 2:
        cout << "Such a jerk, you are." << endl;
            break;

    }

我没有意识到向量的奇迹之一是能够直接访问值 - Ryan Guthrie 在他的评论中提到了这一点,但我找到了一个更简单的“答案”。即:

case 1:
    cout << "Which item would you like to use?" << endl;
    //TODO: Learn what the hell the following line actually means.
    for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
    {
        //Makes a numerical list.
        cout << "Item " << index + 1 << ": " <<  inventory[index].itemName << endl;
        a+= 1;
    }
    cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

    cin >> choice;
    //Cannot define this outside of the statement, or it'll initialize to -1
    invVecPos = (choice - 1);

    //This checks for an invalid response. TODO: Add in non-int checks. 
    if ((invVecPos) >= inventory.size())
    {
        cout << "Choice out of bounds. Stop being a dick." << endl;
    }
    //If the choice is valid, proceed.
    else
    {
        //checking for a certain item type.
        if(inventory[invVecPos].itemType == "ITEM_HEALTHPOT")
        {
            cout << "You used a healing potion!" << endl;
            //this erases the potion, and automagically moves everything up a tick.
            inventory.erase (inventory.begin() + (invVecPos));
        }

        else if(inventory[invVecPos].itemType == "ITEM_FIREPOT")
        {
            cout << "FIERY BALLS OF JOY!" << endl;
        }

        else
        {
            //error-handling! Whee!
            cout << "Invalid Item type" << endl;
        }
    }


    break;
case 2:
    cout << "Why do you have to be so difficult? Pick 1!" << endl;
    break;

谢谢你,Ryan - 在你的推动下,我能够在别处寻找我需要的代码!“固定”代码被大量注释,因此遇到问题的任何其他人都应该能够收集他们需要的内容!

于 2012-11-14T10:40:03.123 回答
0

要添加到 John Dibling 的答案,最后一部分是您正在寻找一个名称,而不是 aItem。

所以它要么需要:

find(inventory.begin(), inventory.end(), healingPotion) != inventory.end();

其中 operator== 定义为:

bool operator==(const aItem& other) const
{
   return itemName == other.itemName;
}

或者你需要让你的 operator== 接受一个字符串:

find(inventory.begin(), inventory.end(), healingPotion.itemName) != inventory.end();

其中 operator== 定义为:

bool operator==(const std::string& name) const
{
   return itemName == name;
}
于 2012-11-12T19:35:26.907 回答