Constructor cannot be defaulted

Node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef NODE_H
#define NODE_H

template <class someType>

class Node{
private:
  someType data;
  Node* next;
public:
  ~Node();
  Node() = delete;                     //Explicitly making the usual constructor as not Default
  Node(const someType&, Node*) = default; //This is default
  friend class SList;
};


#endif 


Node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "Node.h"
template <class someType>

Node::Node(const someType& value, Node* nextnode){
  this->data = value;
  if(nextnode == 0){
    this->next = 0;
  }else{
    this->next = nextnode;
  }
}


When I compile Node.cpp
g++ Node.cpp -std=c++11
It gives the following error
Node.h:13:34: error: ‘Node<someType>::Node(const someType&, Node<someType>*)’ cannot be defaulted
= delete means that that function doesn't exist.
= default means to use the compiler provide implementation of that function.

1
2
3
4
5
6
//Node foo(...)
//Node(const Node &) = delete;
Node bar(foo); //error: class `Node' can't be copied

//Node(const Node &) = default
Node bar(foo); //will shallow-copy the members. 


1
2
3
4
5
6
7
8
9
10
11
12
class asdf{
public:
   asdf(int);
};

asdf qwerty; //error: class `asdf' does not have a constructor that takes no parameters

class asdf{
public:
   asdf() = default; //now it does, and it uses the implementation provided by the compiler.
   asdf(int);
};
Last edited on
In the code supplied you don't need to use either the = default, or the = delete.

It appears that you don't understand the purpose of those statements.

You may want to study this link to see how and when to use these statements:
http://en.cppreference.com/w/cpp/language/default_constructor

http://www.bogotobogo.com/cplusplus/C11/C11_default_delete_specifier.php
So, let's say I want to make Node(const someType&, Node*) the default constructor. Is this possible?? @jlb thanks for the links. I now understand what exactly delete and default does.
So, let's say I want to make Node(const someType&, Node*) the default constructor. Is this possible??

What do you mean by default constructor?

The "default" constructor is the no argument constructor that is supplied by the compiler when you fail to define any constructors. But once you define any constructor this "default" constructor is no longer supplied by the compiler unless you specifically tell the compiler to generate this no argument constructor using the = default.

jib wrote:
In the code supplied you don't need to use either the = default, or the = delete.

It's not necessarily a bad thing to use =delete even if it's not necessary because it makes it clear that it's a conscious design decision.
Topic archived. No new replies allowed.