0

I'm trying to create a hashtable with chaining as the collision management method. I have a custom list to handle that. Here's the error I am running into:

error: constructor for 'Node' must explicitly initialize the member 'content' which does not have a default constructor

Constructor

  Node(const Y &content) {
    this->content = content;
    this->next = nullptr;
    this->prev = nullptr;
  }




1 Answers1

1

Why am I being asked to explicitly initialize a member when I have already created a constructor to do that?

Because this->content = content; is assignment and not initialization. Before entering the constructor body, the content data member(and also others) will be default initialized. But since there is no default ctor for content, we get the mentioned error.


To solve this you can use member initializer list.

Node(const Y &content): content(content), next(nullptr), previous(nullptr)
{

}
Jason
  • 36,170
  • 5
  • 26
  • 60