Daniel
Daniel

Reputation: 151

allocate an std map with list pointer value in c++

I'm trying to allocate a list for each key of my std map, but using the new operator I obtain some errors (no predefined constructor is find and others), why?

My code is like this one:

std::map<QString, *std::list<ExecutionGUIObject*>> execEvtMap;

execEvtMap["t1"] = new std::list<ExecutionGUIObject*>;

Upvotes: 0

Views: 343

Answers (2)

Quentin
Quentin

Reputation: 63124

As stated by Frerich Raabe, it's a minor syntax error in your map declaration. But you gain nothing by allocating the std::list's dynamically, so why seek trouble ? Just use a map of lists.

std::map<QString, std::list<ExecutionGUIObject*>> execEvtMap;

// Creates a new (empty) list for key "t1" if one does not already exist.
void(execEvtMap["t1"]);

// Creates a new list for key "t1", or clears the existing one.
execEvtMap["t1"].clear();

// Erases a key and its list
execEvtMap.erase("t1");

If this maps owns the ExecutionGUIObject's, you'll want to tweak that too :

std::map<QString, std::list<std::unique_ptr<ExecutionGUIObject>>> execEvtMap;

Upvotes: 2

Frerich Raabe
Frerich Raabe

Reputation: 94329

*std::list<ExecutionGUIObject*>

is not a valid type and hence not a valid argument to the std::map template. You probably meant

std::list<ExecutionGUIObject*>*

which means 'pointer to list of pointers to ExecutionGUIObject objects'.

Upvotes: 4

Related Questions