Ruben Martinez Jr.
Ruben Martinez Jr.

Reputation: 3120

C++ Unordered_Map: Receiving "error: expected unqualified-id before ‘[’ token" at Compile

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.

Upvotes: 2

Views: 2514

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

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.

Upvotes: 3

Related Questions