0

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*>;
1
  • Please include the exact text of the errors. Commented Aug 12, 2015 at 7:51

2 Answers 2

4
*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'.

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

Comments

2

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;

2 Comments

and if I have to expose the lists from a class (like a get method) the efficiency of the two methods is the same? @Quentin
@Daniel Such as std::list<...> &getList(QString key); ? Yes, same cost. In fact, you can only see it speed up by cutting down on the dynamic allocations. Plus, no wild owning raw pointers to manage.

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.