Why assignment operator working even when it was forbidden? - c++

I'm struggling with OOP again. I tried to implement signgly linked list, here's the code:
template <typename T>
class node
{
T value;
node* next;
public:
node(const T& n)
{
this->value = n;
this->next = nullptr;
}
~node()
{
//is it ok to leave destructor empty in my case?
}
};
template <typename T>
class list
{
node<T>* head;
node<T>* tail;
public:
list()
{
this->head = nullptr;
this->tail = nullptr;
}
list(const list& arr)
{
*this = list(); //<================== the line I mentioned
node* current_this = this->head;
node* current_arr = this->arr;
while (current_arr != nullptr)
{
current_this = new node(current_arr);
current_arr = current_arr->next;
current_this = current_this->next;
}
}
list(list&& arr)
{
this->head = arr.head;
this->tail = arr.tail;
arr = list(); //<================== the line I mentioned
}
~list()
{
node* current = this->head;
while (current != nullptr)
{
node* next = current->next;
delete current;
current = next;
}
}
};
I wrote some lines without thinking, i.e. arr = list() and *this = list(). I didn't implemented operator= so there should be some troubles. I thought compiler has decided to generate copy/move assigment for me, so I decided to add these lines, just for curiosity sake:
list& operator=(const list& arr) = delete;
list& operator=(list&& arr) = delete;
But it still compiled and I don't understand why. What's the deal here? Shoudn't assignment be forbidden and therefore arr = list() and *this = list() be illegal?

Templates aren't evaluated unless/until you instantiate them. If you don't use the copy or move constructor, then their bugs don't produce errors.

Related

Copy Constructor Error "Terminate called after throwing an instance of 'int'" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
The task was to create a copy constructor for a Forward_list class, and this is the code I have tried. But it keeps showing an error:
terminate called after throwing an instance of 'int'
I have been editing and changing, but I can't seem to get past that error.
class Forward_list
{
public:
class Node
{
public:
// A node will hold data of type T
T data{};
// next will point to the next node in the list
// we initialise next to nullptr
Node* next = nullptr;
// Because we have already intialised the variables
// the default constructor doesn't need to do anything
Node(){}
// if the constructor is called with just one argument.
Node(T input_data, Node* next_node= nullptr)
{
data = input_data;
next = next_node;
}
// Destructor
~Node(){}
};
private:
// private member variables for Forward_list
unsigned size_ = 0;
Node* head_ = nullptr;
public:
Forward_list(const Forward_list<T>& other);
template <typename T>
Forward_list<T>::Forward_list(const Forward_list& other) {
head_ = nullptr;
Node *prev_node = nullptr;
for(Node *other = head_; other != nullptr; other = other->next) {
Node *new_node = new Node;
new_node->data = other->data;
new_node->next = nullptr;
if (!head_)
head_ = new_node;
else
prev_node->next = new_node;
prev_node = new_node;
}
}
Nothing in this code throws an int. So it has to be in code you have not shown. Perhaps in the constructor of T?
Not that it matters, because your copy constructor does not even run its loop at all, since you are looping through the wrong list. You are starting your loop with this->head_ when you need to start it with other.head_ instead.
Also, the Node converting constructor should take its input_data parameter by const reference, and use a member initialization list, to avoid having to default-construct and then assign the data member in separate operations.
Also, the Forward_list copy constructor can be simplified.
Also, make sure you are following the Rule of 3/5/0, if you are not already doing so (you did not show that code).
Try something more like this:
class Forward_list
{
public:
class Node
{
public:
T data;
Node* next;
Node(const T &input_data, Node* next_node = nullptr);
};
Forward_list() = default;
Forward_list(const Forward_list<T>& other);
Forward_list(Forward_list<T>&& other);
~Forward_list();
Forward_list& operator=(Forward_list<T> other);
...
private:
// private member variables for Forward_list
unsigned size_ = 0;
Node* head_ = nullptr;
};
template <typename T>
Forward_list<T>::Node(const T &input_data, Node* next_node)
: data(input_data), next(next_node)
{
}
template <typename T>
Forward_list<T>::Forward_list(const Forward_list& other) {
Node **new_node = &head_;
for(Node *other_node = other.head_; other_node != nullptr; other_node = other_node->next) {
*new_node = new Node(other->data);
++size_;
new_node = &((*new_node)->next);
}
}
template <typename T>
Forward_list<T>::Forward_list(Forward_list&& other) :
head_(std::exchange(other.head_, nullptr)),
size_(std::exchange(other.size_, 0))
{
}
template <typename T>
Forward_list<T>::~Forward_list() {
Node *node = head_;
while (node) {
Node *next = node->next;
delete node;
node = next;
}
}
template <typename T>
Forward_list<T>& Forward_list<T>::operator=(Forward_list other)
{
std::swap(head_, other.head_);
std::swap(size_, other.size_);
return *this;
}

Destructor for a List class in C++

I'm new to C++ thus the question. I've a toy implementation of a Singly Linked List in C++.
template<typename T>
class List {
template<typename U>
struct Node {
U data_;
Node<U>* next_;
Node() : data_(0), next_(nullptr) {}
Node(U data) : data_(data), next_(nullptr) {}
};
private:
Node<T>* head_;
std::size_t size_;
public:
List() : head_{nullptr}, size_{0} {}
void insert(const T& item) {
Node<T>* p(new Node<T>(item));
if (size_ == 0) {
head_ = p;
} else {
p->next_ = head_;
head_ = p;
}
size_++;
}
std::size_t getSize() {
return size_;
}
~List(){
while(head_){
Node<T> p = head_;
delete(p);
head_ = head_->next_;
}
};
This code seems to work. The problem though is that the objects allocated by new are never cleaned up, despite the ~List() destructor. Can someone help me understand, how I can write a destructor for this class that cleans up all the allocated nodes ?
Important remark: I am aware that this can be done using smart pointers, but I want to understand the old school way of managing heap.
while(head_){
Node<T> p = head_; <-- change to pointer
delete(p); <-- you can't delete this right now
head_ = head_->next_;
}
p should be a pointer. You cannot delete p right away. You have to find the next node, and delete p later. Also use delete p; instead of delete (p); as follows:
~List() {
while(head_) {
Node<T> *p = head_;
head_ = head_->next_;
delete p;
}
}
As noted in comments, Node does not need to be a template. You can simplify your class. insert can also be simplified because head_ is initialized to nullptr, you can safely assign p->next_ = head_;
template<typename T> class List {
struct Node {
T data_;
Node* next_;
Node() : data_(0), next_(nullptr) {}
Node(T data) : data_(data), next_(nullptr) {}
};
Node* head_;
std::size_t size_;
public:
List() : head_{ nullptr }, size_{ 0 } {}
void insert(const T& item) {
Node* p = new Node(item);
p->next_ = head_;
head_ = p;
size_++;
}
std::size_t getSize() {
return size_;
}
~List() {
while(head_) {
Node *marked = head_;
head_ = head_->next_;
delete marked;
}
}
};
The general idea is that you have to figure out who is the owner of the object to decide who should delete it.
In relation to nodes, the List is the owner. So you should carefully develop all the methods in a way that once the List looses the ownership of the object, it makes sure that object is deleted or the ownership is taken over.
Obvious places when you want to free the memory is, first one you delete the list. Second, when you remove an element, for example pop it.
Lets look in both cases.
First delete the list. For that you need to write a destructor, which iterates over the list and deletes elements one by one. For that I refer to the answer of #barmak-shemiani.
For the case when you pop an element you can do following:
T pop() {
Node<T> *tmp = head_;
if (head_ != nullptr) {
head_ = head_->next_;
T data = tmp->data_;
delete tmp;
return data;
}
throw std::runtime_error("Can't pop an empty list")
}

Linked List ostream overload weird values

I'm realitively new to using linked lists. I'm trying to overload the ostream opperator for a doubly linked list with decleration :
template <class T>
class DynamicList;
template <class T>
template <class T>
class DynamicList
{
private:
Node<T> *head;
public:
class Node
{
public:
T* value;
Node<T> *next;
Node<T> *prev;
Node(T val)
{
next = nullptr;
prev = nullptr;
value = &val;
}
};
DynamicList();
~DynamicList();
void operator+=(const T);
friend std::ostream & operator<< <>(std::ostream &, const DynamicList<T> &);
};
and function defenition:
template <class T>
ostream& operator << (ostream & out , const DynamicList<T> & rhs)
{
Node<T>* nodePtr = rhs.head;
Node<T>* nptr = nodePtr->next;
while(nodePtr != NULL)
{
cout<<*(nodePtr->value)<<" ";
nodePtr = nodePtr->next;
}
out<<endl;
return out;
}
template <class T>
void DynamicList<T>:: operator +=(const T val)
{
Node<T>* nodePtr = nullptr;
T vall = val;
Node<T>* newNode = new Node<T>(vall);
if(!head)
{
head = newNode;
}
else
{
nodePtr = head;
while((nodePtr->next))
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
newNode->prev = nodePtr;
}
Every time I'm calling the opperator it gives a weird output for example using:
for(int i = 1; i <= 3; i++)
{
list += i;
}
cout<<list;
It would give an output like 135727363 135727383 135727383 ,I'd just like to know what I'm doing wrong and possibly how I could solve it
Your problem is here:
T* value;
You are storing the address of a value.
The problem is that you are storing the address of a variable that has gone out of scope.
T vall = val;
Node<T>* newNode = new Node<T>(vall);
The variable vall is local to the function operator += which means after it exists it no longer exists (and can contain anything).
To fix change the node to store the value.
class Node
{
public:
T value; // Notice no star here.
Node<T> *next;
Node<T> *prev;
Node(T const& val) // Add `const&` here to avoid a copy.
{
next = nullptr;
prev = nullptr;
value = val; // Notice no and `&` here
}
};

Destructing a linked list

I was trying to implement a linked list for solving an algorithm problem.
It basically worked, however, it turned out that I was using too much memory.
I would appreciate if someone point out defects of following destructor design.
template<typename T>
struct Node {
Node(): item(0),next(0) {}
Node(T x): item(x),next(0) {}
T item;
Node* next;
};
template <typename T>
struct List {
List() : head(0),tail(0) {}
Node<T>* head;
Node<T>* tail;
void insert(T x) {
Node<T>* newNode = new Node<T>(x);
if(head == NULL) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = tail->next;
}
}
void clearRecur(Node<T>* h) {
if(h) {
clearRecur(h->next);
delete h;
}
}
void clear() {
if(head) {
clearRecur(head);
}
}
};
A list can be cleared recursively or iteratively.
Alternatively to your (IMHO correct) version, I use a slight different approach – make the Node itself "responsible" to delete its tail. This leads to recursive clearing as well (but with less code).
Recursive clearing:
template<typename T>
struct Node {
Node(): item(), next(nullptr) {}
Node(T x): item(x), next(nullptr) {}
~Node() { delete next; } // <== recursive clearing
T item;
Node* next;
// Using the default copy ctor would corrupt the memory management.
// Deleting it lets the compiler check for accidental usage.
Node(const Node&) = delete;
// Deleting assignment operator as well.
Node& operator=(const Node&) = delete;
};
template <typename T>
struct List {
List() : head(nullptr), tail(nullptr) {}
~List() { clear(); }
Node<T>* head, tail;
void insert(T x) {
Node<T>* newNode = new Node<T>(x);
if (head == nullptr) head = tail = newNode;
else {
tail->next = newNode;
tail = tail->next;
}
}
void clear() {
delete head;
head = tail = nullptr;
}
// Using the default copy ctor would corrupt the memory management.
// Deleting it lets the compiler check for accidental usage.
List(const List&) = delete;
// Delete assignment operator as well.
List& operator=(const List&) = delete;
};
This is the way, I did it in our current project. At the first glance, it seemed enjoying simple and worked fine. I changed my mind when our beta-testers came into play. In real world projects, the lists were such long that the recursive clearing ran out of stack memory. (Yepp – a stack overflow.) I should've known better!
Thus, I made the clearing iteratively – whereby the "responsibility" is moved back from Node to List. (The API user will not note this as it happens "under the hood".)
Iterative clearing:
template<typename T>
struct Node {
Node(): item(), next(nullptr) {}
Node(T x): item(x), next(nullptr) {}
T item;
Node* next;
};
template <typename T>
struct List {
List() : head(nullptr), tail(nullptr) {}
~List() { clear(); }
Node<T>* head, tail;
void insert(T x) {
Node<T>* newNode = new Node<T>(x);
if (head == nullptr) head = tail = newNode;
else {
tail->next = newNode;
tail = tail->next;
}
}
void clear() {
while (head) {
Node<T> *p = head; head = head->next;
delete p;
}
tail = nullptr;
}
// Using the default copy ctor would corrupt the memory management.
// Deleting it lets the compiler check for accidental usage.
List(const List&) = delete;
// Delete assignment operator as well.
List& operator=(const List&) = delete;
};

Using a custom class with a custom template container

I'm trying to define a custom template container, and then add custom class objects (Students) into it. Here's my code:
class Student{
public:
string subject;
Student(string _subject){
subject = _subject;
}
};
And here's my LinkedList template code
template <class T>
class LinkedList{
private:
struct Node{
Node *next;
T value;
};
Node *root;
Node *curr;
int count;
public:
LinkedList() : count(0), root(NULL){}
void add(T val){
if (root == NULL){
root = new Node;
root->value = val;
root->next = NULL;
curr = root;
}
else{
curr->next = new Node;
curr = curr->next;
curr->value = val;
curr->next = NULL;
}
count++;
}
void print(){
for (Node *itr=root; itr != NULL; itr = itr->next){
cout << itr->value << endl;
}
}
};
int main(){
LinkedList<Student> a;
Student sam("Math");
a.add(sam)
}
When I run it, I get
linkedlist.cpp: In constructor ‘LinkedList::Node::Node()’:
linkedlist.cpp:27: instantiated from ‘void LinkedList::add(T) [with T = Student]’
linkedlist.cpp:133: instantiated from here
linkedlist.cpp:27: error: no matching function for call to ‘Student::Student()’
linkedlist.cpp:18: note: candidates are: Student::Student(std::string)
linkedlist.cpp:15: note: Student::Student(const Student&)
linkedlist.cpp: In member function ‘void LinkedList::add(T) [with T = Student]’:
linkedlist.cpp:40: note: synthesized method ‘LinkedList::Node::Node()’ first required here
I have no clue what that error really means, and what it's asking me to do. If I do this instead:
int main(){
LinkedList<Student*> a;
Student sam("Math");
a.add(&sam);
}
It works just fine.
But storing references to objects is not what I'm after. How can I get my LinkedList to make copies of the object I wish to add to it?
Thanks in advance!
First of all...
#include <utility>
Now, change this...
struct Node {
Node* next;
T value;
};
For...
struct Node {
Node* next;
T value;
inline Node(const T &value) : value(value), next(nullptr) {}
inline Node(T &&value) : value(std::move(value)), next(nullptr) {}
};
And, this...
void add(T val) {
if(this->root == nullptr) {
this->root = new Node;
this->root->value = val;
root->next = NULL;
curr = root;
} else {
curr->next = new Node;
curr = curr->next;
curr->value = val;
curr->next = NULL;
}
count++;
}
For this...
void add(const T &val){
if(this->root == nullptr) {
this->root = new Node(val);
this->curr = root;
} else {
this->curr->next = new Node(val);
this->curr = this->curr->next;
}
this->count++;
}
void add(T &&val){
if(this->root == nullptr) {
this->root = new Node(std::move(val));
this->curr = root;
} else {
this->curr->next = new Node(std::move(val));
this->curr = this->curr->next;
}
this->count++;
}
What happens is that you're creating a Node object with new Node with it's implicit default constructor. That implies that the default constructor for Node::value is called. Then, can you tell me if there's any? No. Student does not have a default constructor, so this won't work.
BTW, I made some minor redesigns to your code, so to avoid several issues that appeared by the way. As a bonus, you can now use move semantics (try list.add(Student("Joe")))!
Also, remember to initialize curr to nullptr in the constructor initialization list!
Student must provide a default constructor. Try that:
Student(string _subject = "")
Because when you do new Node it creates a Node object, so it creates the Node* and T attributes of the Node class. And if T class has no default constructor, the compiler does not know how to create the object.
That's what the error message says:
error: no matching function for call to ‘Student::Student()’ linkedlist.cpp:18: note: candidates are: Student::Student(std::string)
It cannot find a default constructor Student::Student(), it only found one taking a string as argument Student::Student(std::string)