-1

I have this lines:

class ModulePeople : public Module {
public:
   std::list<People> & list_people;
   std::list<People>::iterator it;
}

When I try to compile I have this error:

Compiler Error C2758 
'ModulePeople': a member of reference type must be initialized

This error appears in this line for example:

it = list_people.begin();

But I don't really know how to inicializate this kind of variable, because i can't do it to NULL. If somebody can help me it would be very grateful.

Seifil
  • 21
  • 7

1 Answers1

0

A reference is an alias for an existing object with storage space somewhere. This means that a reference must reference (no pun intended) an existing object, it's not like a pointer which can has value of nullptr.

This implies that if you use a reference as a member field of a class definition then you must initialize it through any available constructor of such object, eg:

class ModulePeople : public Module {
public:
   std::list<People> & list_people;
   std::list<People>::iterator it;

   ModulePeople(decltype(list_people) list_people) : list_people(list_people) { }
}

If you really want to be able to let it point to nothing then could use a pointer instead, eg std::list<People>*.

Jack
  • 131,802
  • 30
  • 241
  • 343