2

I have an unordered_map (in C++) that pairs an int with an object of class Item. I initialize my unordered map at the beginning of the file as such:

#include <iostream>
#include <unordered_map>
using namespace std;
typedef std::unordered_map<int, Item> MyList;

From then on, whenever I try to use MyList, such as in:

Item item1;
MyList[12] = item1;

I receive an error at said line: "error: expected unqualified-id before ‘[’ token" when I compile in the terminal. Any ideas what could be wrong? Below is an other example of how I use it and receive the same or similar error.

void itemManager::removeItem(int x)  {
    MyList.erase(x);
}

Yields: "error: expected primary-expression before ‘.’ token"

Please and thanks for the help.

1
  • 1
    MyList[12] is equivalent to saying std::unordered_map<int, Item>[12]. Commented Jun 29, 2012 at 5:48

1 Answer 1

3

MyList is a type:

typedef std::unordered_map<int, Item> MyList;

but you are using it like an object:

MyList[12] = item1;

Perhaps putting the typedef in there was the mistake.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.