Jay C
Jay C

Reputation: 15

How to create a list inside a map where both contain pointers?

I'm doing a project which requires

class course;
void add_student(map<int, map<int, list<course *> * > *> &DB, int id);

so when I check the course is not presenting, I want to create a list. Here is my code,

if(semesterIt == studentIt->second->end()){
DB[id][semester] = new list<course *>();
}

But when I run it, the compiler give me this error

no viable overloaded '='

No idea how to fix it. :(

Upvotes: 0

Views: 71

Answers (1)

R Sahu
R Sahu

Reputation: 206697

DB[id][semester] = new list<course *>();

is syntactically wrong since DB[id] evaluates to a pointer, not an object or a reference.

My suggestion:

auto& mapPtr = DB[id];
if ( mapPtr == nullptr )
{
  mapPtr = new map<int, list<course *> * >;

  // Not necessary since mapPtr is a reference to the element.
  // DB[id] = mapPtr;
}

auto& course_list_ptr = (*mapPtr)[semester];
if ( course_list_ptr == nullptr ) 
{
   course_list_ptr = new list<course*>;

   // Again, not necessary.
   // (*mapPtr)[semester] = course_list_ptr;
}

Upvotes: 1

Related Questions