9T9
9T9

Reputation: 698

Copy Object in C++

    class Item
    {
        string sItemName;
        int iQt, iPrice;

    public:
        Item(string, int, int);
        int getPrice(int);
     };


    Item o_FindItem;        

    int ItemCSVImport::GetItem(char* cItemName)
    {
        o_FindItem = (m_ItemsMap.find(cItemName)->second);
        return 1;
     }

The above code work fine in the first time. But when I try to call the same method for the second time the first line inside the method gives an segmentation fault. Can anyone suggest a solution?

Upvotes: 0

Views: 61

Answers (1)

John Zwinck
John Zwinck

Reputation: 249123

The problem is probably that cItemName is not found. To fix it, try something like this:

int ItemCSVImport::GetItem(char* cItemName)
{
    auto it = m_ItemsMap.find(cItemName);
    if (it != m_ItemsMap.end()) {
        o_FindItem = it->second;
        return 1;
    } else {
        return 0;
    }
}

Upvotes: 3

Related Questions