My linkedlist print function goes into infinite loop - c++

linkedlist.h
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include <iostream>
template <typename T>
class list{
public:
list();
class node;
typedef T value_type;
typedef T& reference;
typedef node node_type;
typedef node_type* node_pointer;
list& push_back(value_type);
value_type pop_front();
node_pointer begin() const;
private:
node_pointer head, tail;
};
template <typename T>
class list<T>::node{
public:
node(value_type value = 0);
void setData(value_type);
value_type getData() const;
void setNext(node_pointer);
node_pointer getNext() const;
private:
value_type data;
node_pointer ptr_next;
};
template <typename T>
inline list<T>::node::node(value_type value) : data(value), ptr_next(nullptr){
}
template <typename T>
void list<T>::node::setData(value_type value){
this->data = value;
}
template <typename T>
typename list<T>::value_type list<T>::node::getData() const{
return this->data;
}
template <typename T>
void list<T>::node::setNext(node_pointer ptr){
this->ptr_next = ptr;
}
template <typename T>
typename list<T>::node_pointer list<T>::node::getNext() const{
return this->ptr_next;
}
template <typename T>
list<T>::list() : head(nullptr), tail(nullptr){
}
template <typename T>
list<T>& list<T>::push_back(value_type value){
node_pointer item = new node_type(value);
if(this->tail) this->tail->setNext(item);
this->tail = item;
if(!this->head) this->head = this->tail;
return *this;
}
template <typename T>
typename list<T>::value_type list<T>::pop_front(){
if(this->head){
node_pointer item = this->head;
value_type value = this->head->getData();
this->head = this->head->getNext();
delete item;
return value;
}
return 0;
}
template <typename T>
typename list<T>::node_pointer list<T>::begin() const{
return this->head;
}
template <typename T>
std::ostream & operator<<(std::ostream &stream, list<T> &obj){
typename list<T>::node_pointer ptr = obj.begin();
while(ptr->getNext() != nullptr){
stream << ptr->getData() << " ";
ptr->setNext(ptr->getNext());
}
return stream;
}
#endif /* LINKEDLIST_H_ */
main.cpp
#include <iostream>
#include "LinkedList.h"
using std::cout;
using std::endl;
int main (int argc, char ** argv){
list<int> mylist;
mylist.push_back(5).push_back(1);
cout << mylist;
return 0;
}
I just wanted to create a print function which overloads operator<<. whatever i did, i couldn't find the solution. What is the wrong with this overloaded operator<< function? And how can i clean garbage if program throws error. How destructor function should be?

template <typename T>
std::ostream & operator<<(std::ostream &stream, list<T> &obj){
typename list<T>::node_pointer ptr = obj.begin();
while(ptr != nullptr){
stream << ptr->getData() << " ";
ptr = ptr->getNext();
}
return stream;
}
i realized that i made a logical mistake during the print function. But i still wonder how to collect garbage?

Related

template class header file

I'm trying to implement stack data structure using singly linked list.
This is the header file that has template class of Node and List
#pragma once
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
private:
SNode<E>* head;
public:
SLinkedList();
~SLinkedList();
bool empty() const ();
const E& front() const;
void addFront(const E& e);
void removeFront();
};
template <typename E>
SLinkedList<E>::SLinkedList() : head(NULL) {}
template <typename E>
SLinkedList<E>::~SLinkedList() {
while(!empty()) removeFront();
}
template <typename E>
bool SLinkedList<E>::empty() const {
return head==NULL;
}
template <typename E>
const E& SLinkedList<E>::front() const {
return head->elem;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* tmp = new SNode<E>;
tmp->elem = e;
tmp->next=head;
head=tmp;
}
template <typename E>
void SLinkedList<E>::removeFront() {
SNode<E>* old = head;
head=head->next;
delete old;
}
This is a simple main code
#include <iostream>
#include "SNode.h"
using namespace std;
int main() {
SNode<int> A(1);
SNode<int> B(2);
SLinkedList<int> L();
L.push(A);
L.push(B);
return 0;
}
When I compile this code the compiler says
SLinkedList is not a class template
I have no idea why it says that since I did put
template
You need to forward declare the SLinkedList templated class. like this:-
template <typename E>
class SLinkedList;
template <typename E>
class SNode {
E elem;
SNode<E>* next;
public:
friend class SLinkedList<E>;
};
...
There are other errors in you code. I hope you are able to resolve them by youself.
You have vexing parse with
SLinkedList<int> L();
Which declares a.. function L, taking no parameters, and returning SLinkedList<int>.
Use
SLinkedList<int> L;
or
SLinkedList<int> L{};

How to initialize values of Singly Linked List with constructor [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I have to create a constructor that allows a new linked list to be populated with ten consecutive values, starting at 0. Then I need to print the list! So i want to check if the functions I wrote for those are ok. Thanks!!
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
template <typename E>
class SNode {
private:
E elem;
SNode<E> *next;
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList {
public:
SLinkedList();
SLinkedList(SNode<E>* v); //What I need help with
~SLinkedList();
bool empty() const;
E& front();
void printList(SLinkedList<E> &list); //what i need help with
void addFront(const E& e);
void removeFront();
int size() const;
private:
SNode<E>* head;
int n; // number of items
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(0) { }
template <typename E>
SLinkedList<E>::SLinkedList(SNode<E>* v){ //WHat I Need Help With
SNode<E>* v = new SNode<E>;
for (int i = 0; i < 10; i++)
v->elem = i;
}
template <typename E>
bool SLinkedList<E>::empty() const
{
return head == NULL; // can also use return (n == 0);
}
template <typename E>
E& SLinkedList<E>::front()
{
if (empty()) throw length_error("empty list");
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList()
{
while (!empty()) removeFront();
}
template<typename E>
void SLinkedList<E>::printList(SLinkedList<E> &list) //What I need help with
{
for (int i = 0; i < list.size(); i++)
{
cout << list << " ";
}
cout << endl;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
n++;
}
template <typename E>
void SLinkedList<E>::removeFront() {
if (empty()) throw length_error("empty list");
SNode<E>* old = head;
head = old->next;
delete old;
n--;
}
template <typename E>
int SLinkedList<E>::size() const {
return n;
}
thanks in advance for any help or suggestions! It is just these two functions that i am not sure about.
Try something more like this:
#include <iostream>
#include <string>
#include <stdexcept>
#include <initializer_list> // C++11 and later only
template <typename E>
class SLinkedList {
public:
SLinkedList();
SLinkedList(const E *vals, int num_vals);
SLinkedList(std::initializer_list<E> vals); // C++11 and later only
~SLinkedList();
bool empty() const;
int size() const;
E& front();
void addFront(const E &e);
void removeFront();
void printList() const;
private:
class SNode
{
public:
E elem;
SNode *next;
SNode(const E &e, SNode *n = NULL);
};
SNode* head;
int n; // number of items
};
template <typename E>
SLinkedList<E>::SNode::SNode(const E &e, SLinkedList<E>::SNode *n)
: elem(e), next(n) { }
template <typename E>
SLinkedList<E>::SLinkedList()
: head(NULL), n(0) { }
template <typename E>
SLinkedList<E>::SLinkedList(const E *vals, int num_vals)
: head(NULL), n(0)
{
for (int i = num_vals-1; i >= 0; --i)
addFront(vals[i]);
/* alternatively:
SNode **ptr = &head;
for (int i = 0; i < num_vals; ++i)
{
*ptr = new SNode(vals[i]);
++n;
ptr = &((*ptr)->next);
}
*/
}
template <typename E>
SLinkedList<E>::SLinkedList(std::initializer_list<E> vals)
: head(NULL), n(0)
{
const E *begin = vals.begin(), *iter = vals.end();
while (iter != begin)
addFront(*(--iter));
/* alternatively:
const E *iter = vals.begin(), *end = vals.end();
SNode **ptr = &head;
while (iter != end)
{
*ptr = new SNode(*iter);
++n;
ptr = &((*ptr)->next);
}
*/
}
template <typename E>
SLinkedList<E>::~SLinkedList()
{
while (head)
removeFront();
}
template <typename E>
bool SLinkedList<E>::empty() const
{
return (!head);
}
template <typename E>
int SLinkedList<E>::size() const
{
return n;
}
template <typename E>
E& SLinkedList<E>::front()
{
if (!head) throw std::length_error("empty list");
return head->elem;
}
template<typename E>
void SLinkedList<E>::printList() const
{
SNode *p = head;
while (p)
{
std::cout << p->elem << " ";
p = p->next;
}
std::cout << std::endl;
}
template <typename E>
void SLinkedList<E>::addFront(const E& e)
{
head = new SNode(e, head);
++n;
}
template <typename E>
void SLinkedList<E>::removeFront()
{
SNode* old = head;
if (!old) throw std::length_error("empty list");
head = old->next;
--n;
delete old;
}
Live Demo

a function call changes it's parameters

I have the following code:
#include <iostream>
using namespace std;
template <class T>
class Iterator;
template <class T>
class List;
template <class T>
class List {
public:
struct Node;
Node* first;
friend class Iterator<T>;
List() :
first(NULL) { }
Iterator<T> begin() {
cout << first->data << endl;
return Iterator<T>(*this, first); // <--- problematic call
}
void insert(const T& data) {
Node newNode(data, NULL);
first = &newNode;
}
};
template <class T>
struct List<T>::Node {
private:
T data;
Node* next;
friend class List<T>;
friend class Iterator<T>;
Node(const T& data, Node* next) :
data(data), next(next) { }
};
template <class T>
class Iterator {
private:
const List<T>* list;
typename List<T>::Node* node;
friend class List<T>;
public:
Iterator(const List<T>& list, typename List<T>::Node* node) {
cout << node->data << endl;
}
};
int main() {
List<int> list;
list.insert(1);
list.begin();
return 0;
}
First I set the node data to "1" (int). Ater that I just pass it to the Iterator constructor, but it changes the value of node->data.
I printed node->data before and after the call:
1
2293232
I guess that 2293232 is an address of something, but I can't find the reason this happens.
When you write
void insert(const T& data) {
Node newNode(data, NULL);
first = &newNode;
}
Then:
You create an object on the stack
Point some (more) persistent pointer to its address
Destruct it as it goes out of scope
So you're left with garbage stuff.

Template LinkedList class with nested class

I'm trying to implement a linked list with a template class LList and Nested Iterator and Node classes. Here's the code:
template <typename T1>
class LList
{
public:
class Iterator
{
public:
Iterator();
T1 get() const;
void next();
void previous();
bool equals(Iterator iter) const;
private:
Node* position;
LList* container;
};
LList();
~LList();
void pushBack(T1 data);
Iterator begin();
Iterator end();
void insert (Iterator iter, T1 data);
Iterator erase(Iterator Iter);
private:
class Node
{
public:
Node(T1 data);
private:
T1 data;
Node* ptr_next;
Node* ptr_prev;
};
Node* ptr_first;
Node* ptr_last;
};
template <typename T1>
LList<T1>::Node::Node(T1 data)
{
this->data = data;
ptr_next = 0;
ptr_prev =0;
}
template <typename T1>
LList<T1>::Iterator::Iterator()
{
position = 0;
container = 0;
}
template <typename T1>
T1 LList<T1>::Iterator::get() const
{
return position->data;
}
template <typename T1>
void LList<T1>::Iterator::next()
{
if(position == container->ptr_last)
{
position = container->ptr_first;
}
else
{
position = position->ptr_next;
}
}
template <typename T1>
void LList<T1>::Iterator::previous()
{
if(!position)
{
position = container->ptr_last;
}
else
{
position = position->ptr_prev;
}
}
template <typename T1>
bool LList<T1>::Iterator::equals(Iterator iter) const
{
return position == iter.position;
}
template <typename T1>
LList<T1>::LList()
{
ptr_first = 0;
ptr_last = 0;
}
template <typename T1>
LList<T1>::~LList()
{
while (ptr_first)
{
Node* tmp = ptr_first;
ptr_first = ptr_first->ptr_next;
delete tmp;
tmp = 0;
}
}
template <typename T1>
void LList<T1>::pushBack(T1 data)
{
Node* new_node = new Node(data);
if(ptr_first==0)
{
ptr_first = new_node;
ptr_last = new_node;
}
else
{
ptr_last->ptr_next = new_node;
new_node->ptr_prev = ptr_last;
ptr_last = new_node;
}
}
template <typename T1>
Iterator LList<T1>::begin()
{
Iterator iter;
iter.positon = ptr_first;
iter.container = this;
return iter;
}
template <typename T1>
Iterator LList<T2>::end()
{
Iterator iter;
iter.position = ptr_last;
iter.container = this;
return iter;
}
template <typename T1>
void LList<T1>::insert(Iterator iter, T1 data)
{
if (iter.position == 0)
{
pushBack(data);
return;
}
Node* before;
Node* after;
after = iter.position;
before = iter.position->ptr_prev;
Node* new_node = new Node(data);
after->ptr_prev = new_node;
if (before == 0) ptr_first = new_node;
else before->ptr_next = new_node;
new_node->ptr_prev = before;
new_node->ptr_next = after;
}
template <typename T1>
Iterator LList<T1>::erase(Iterator iter)
{
Node* after = iter.position->ptr_next;
Node* before = iter.position->ptr_prev;
Node* remove = iter.position;
if (remove == ptr_first) ptr_first = after;
else before->ptr_next = after;
if (remove == ptr_last) ptr_last = before;
else after->ptr_prev = before;
delete remove;
remove = 0;
}
I've seen how do it without nested classes but I need to do it with a nested class.
Any help on why it doesn't compile will help :) Thanks.
Well, Iterator is the name of a nested class, so when you use it in the definition of a member function of your LList class template as the return type, you have to fully qualify it (and add the typename disambiguator to tell the compiler that what follows the :: shall be parsed as the name of a type).
For instance:
template <typename T1>
typename LList<T1>::Iterator LList<T1>::erase(Iterator iter)
// ^^^^^^^^^^^^^^^^^^^^
There are several instances of this error, so you will have to fix all of them.
You are also referring to class Node before its definition appears inside LList. Therefore, you should have a forward declaration for it:
template <typename T1>
class LList
{
class Node;
// ^^^^^^^^^^^
// Forward declaration for Node
public:
// ...
class Iterator
{
// ...
Node* position; // <== Now this is OK because of the forward declaration
// ...
};
There are different error :
The first is that you use Node into the Iterator class, but you need to declare Node before it :
template <typename T1>
class LList
{
private:
class Node
{
public:
Node(T1 data);
private:
T1 data;
Node* ptr_next;
Node* ptr_prev;
};
Node* ptr_first;
Node* ptr_last;
public:
LList();
~LList();
void pushBack(T1 data);
class Iterator
{
public:
Iterator();
T1 get() const;
void next();
void previous();
bool equals(Iterator iter) const;
private:
Node* position;
LList* container;
};
Iterator begin();
Iterator end();
void insert (Iterator iter, T1 data);
Iterator erase(Iterator Iter);
};
That is the first thing.
The second is that Iterator is a nested type, so you need to be more specific when you return un object of this type :
template <typename T1>
typename LList<T1>::Iterator LList<T1>::begin()
And the last error is :
template <typename T1>
Iterator LList<T2>::end()
Here is the correct lign :
template <typename T1>
typename LList<T1>::Iterator LList<T1>::end()
// ^^^^^^^^^^^^^^^^^ ^^

Binary Heap No Matching Function Error

I am working on a linked list type implementation of a binary heap, and as it is written I am getting some errors. Right now my main.cpp is a simple test for adding an element to the heap, but when I call my "add to heap" function it says it can't find it. Here is the code:
main.cpp
#include <QtCore/QCoreApplication>
#include "Queue.h"
#include "Heap.h"
#include <bitset>
#include <cmath>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Heap<int> H;
H.AddToHeap(1);
return a.exec();
}
Heap.h
#ifndef HEAP_H
#define HEAP_H
#include "Node.h"
#include <iostream>
#include <bitset>
#include <cmath>
enum BOUNDARY_ERRORS{OUT_OF_BOUNDS, INVALID_NODE};
template <typename T>
class Heap
{
public:
Heap();
Heap(const Heap &other);
void operator=(const Heap &other);
~Heap();
void AddToHeap(T &data);
Heap& operator<<(T &data);
T& Pop();
Heap& operator>>(T &destination);
bool Empty() { return size==0; }
T Peek();
template <typename U> friend
ostream& operator<<(const ostream &out, const Heap<U> &H);
template <typename U> friend
istream& operator>>(const istream &in, const Heap<U> &H);
private:
int size;
Node<T> *rootptr;
void AddToVacantNode(T &data);
Node<T>* FindNode(int n);
Node<T>* FindParentNode(int n);
Node<T>* LargestChild(Node<T> *nodeptr);
Node<T>* SmallestChild(Node<T> *nodeptr);
void Upheap();
void Downheap();
void Switch(Node<T> *a, Node<T> *b);
void Replace(Node<T> *a, Node<T> *b);
void Copy(const Heap &other);
bool MIN;
void Clear();
};
template <typename T>
Heap<T>::Heap()
{
size = 0;
rootptr = NULL;
MIN = 0;
}
template <typename T>
Heap<T>::Heap(const Heap<T> &other)
: Heap()
{
Copy(other);
}
template <typename T>
void Heap<T>::operator=(const Heap<T> &other)
{
Copy(other);
}
template <typename T>
Heap<T>::~Heap()
{
Clear();
}
template <typename T>
void Heap<T>::AddToVacantNode(T &data)
{
if (Empty())
rootptr = new Node<T>(data);
else
{
int destination = size + 1;
Node<T> newnode(data);
Node<T> *parentptr = FindParentNode(destination);
if (!destination%2)
parentptr->AddLeftChild(newnode);
else
parentptr->AddRightChild(newnode);
}
}
template <typename T>
Node<T>* Heap<T>::FindParentNode(int n)
{
if (n == 1)
return NULL;
int parentnumber;
if (!n%2)
{
parentnumber = n / 2;
Node<T> *nodeptr = FindNode(parentnumber);
return nodeptr;
}
else
{
parentnumber = (n - 1) / 2;
Node<T> *nodeptr = FindNode(parentnumber);
return nodeptr;
}
}
template <typename T>
void Heap<T>::Upheap()
{
Node<T> *parentptr = FindParentNode(size);
Node<T> *childptr = FindNode(size);
if (MIN)
{
while (parentptr && *childptr < *parentptr)
{
switch(parentptr, childptr);
parentptr = FindParentNode(parentptr);
childptr = FindParentNode(childptr);
}
return;
}
else
{
while (parentptr && *childptr > *parentptr)
{
switch(parentptr, childptr);
parentptr = FindParentNode(parentptr);
childptr = FindParentNode(childptr);
}
return;
}
}
template <typename T>
Node<T>* Heap<T>::LargestChild(Node<T> *nodeptr)
{
if (!nodeptr->LeftChild() && !nodeptr->RightChild())
return NULL;
else if (nodeptr->LeftChild() && !nodeptr->RightChild())
return nodeptr->LeftChild();
else if (nodeptr->RightChild() && !nodeptr->LeftChild())
return nodeptr->RightChild();
else
return (*(nodeptr->LeftChild() > *(nodeptr->RightChild())))?
nodeptr->LeftChild() : nodeptr->RightChild();
}
template <typename T>
Node<T>* Heap<T>::SmallestChild(Node<T> *nodeptr)
{
if (!nodeptr->LeftChild() && !nodeptr->RightChild())
return NULL;
else if (nodeptr->LeftChild() && !nodeptr->RightChild())
return nodeptr->LeftChild();
else if (nodeptr->RightChild() && !nodeptr->LeftChild())
return nodeptr->RightChild();
else
return (*(nodeptr->LeftChild() < *(nodeptr->RightChild())))?
nodeptr->LeftChild() : nodeptr->RightChild();
}
template <typename T>
void Heap<T>::Downheap()
{
Node<T> *nodeptr = FindNode(size);
*rootptr = *nodeptr;
}
template <typename T>
void Heap<T>::Replace(Node<T> *a, Node<T> *b)
{
a->Data() = b->Data();
b->NullPtrs();
Node<T> *parentptr = FindParentNode(b);
if (parentptr->LeftChild() = b)
{
parentptr->NullLeftChild();
delete b;
b = NULL;
}
else
{
parentptr->NullRightChild();
delete b;
b = NULL;
}
return;
}
template <typename T>
void Heap<T>::AddToHeap(T &data)
{
AddToVacantNode(data);
Upheap();
size++;
}
template <typename T>
Heap<T>& Heap<T>::operator<<(T &data)
{
AddToHeap(data);
return *this;
}
template <typename T>
T& Heap<T>::Pop()
{
return rootptr->Data();
Downheap();
size--;
}
template <typename T>
Heap<T>& Heap<T>::operator>>(T &destination)
{
destination = rootptr->Data();
Downheap();
size--;
}
template <typename T>
T Heap<T>::Peek()
{
if (!Empty())
return rootptr->Data();
}
template <typename T>
ostream& operator<<(const ostream &out, const Heap<T> &H)
{
return;
}
template <typename T>
istream& operator>>(const istream &in, const Heap<T> &H)
{
return;
}
template <typename T>
void Heap<T>::Switch(Node<T> *a, Node<T> *b)
{
T temp;
temp = a->Data();
a->SetData(b->Data());
b->SetData(temp);
}
template <typename T>
void Heap<T>::Copy(const Heap &other)
{
if (this != &other && !other.Empty())
{
MIN = other.MIN;
Node<T> *nodeptr;
Clear();
for (int n=1; n<=other.size; n++)
{
nodeptr = other.FindNode(n);
AddToHeap(nodeptr->data);
}
}
}
template <typename T>
Node<T>* Heap<T>::FindNode(int n)
{
if (n > size || n < 1)
throw OUT_OF_BOUNDS;
int x = floor(log(n)/log(2)+1);
bitset<20> bs(n);
Node<T> *nodeptr = rootptr;
for (int i=x-2; i>=0; i--)
{
if (!bs[i])
nodeptr = nodeptr->LeftChild();
else
nodeptr = nodeptr->RightChild();
}
return nodeptr;
}
template <typename T>
void Heap<T>::Clear()
{
for (int n=size; n>0; n++)
{
Node<T> *nodeptr = FindNode(n);
nodeptr->NullPtrs();
delete nodeptr;
}
rootptr->NullPtrs();
delete rootptr;
}
#endif // HEAP_H
Node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
template <typename T>
class Node
{
public:
Node();
Node(T &DATA);
Node(const Node<T> &other);
Node<T>& operator=(const Node<T> &other);
Node<T>& operator<<(const T &nodedata);
bool operator==(const Node<T> &other);
bool operator<(const Node<T> &other);
bool operator>(const Node<T> &other);
bool operator<=(const Node<T> &other);
bool operator>=(const Node<T> &other);
bool operator!=(const Node<T> &other);
~Node();
T Data() const { return data; }
void SetData(const T &nodedata);
void AddLeftChild(const Node<T> *leftchildptr);
void AddRightChild(const Node<T> *rightchildptr);
Node<T> *LeftChild() { return leftchild; }
Node<T> *RightChild() { return rightchild; }
void NullLeftChild() { leftchild = NULL; }
void NullRightChild() { rightchild = NULL; }
void NullPtrs() { leftchild = rightchild = NULL; }
template <typename U> friend
ostream& operator<<(ostream &out, Node<U> &node);
private:
T data;
Node<T> *leftchild;
Node<T> *rightchild;
void Copy(const Node<T> &other);
};
template <typename T>
Node<T>::Node()
{
NullPtrs();
return;
}
template <typename T>
Node<T>::Node(T &DATA)
{
NullPtrs();
data = DATA;
return;
}
template <typename T>
Node<T>::Node(const Node<T> &other)
{
Copy(other);
return;
}
template <typename T>
Node<T>& Node<T>::operator=(const Node &other)
{
if (this != &other)
Copy(other);
return this;
}
template <typename T>
Node<T>& Node<T>::operator<<(const T &nodedata)
{
SetData(nodedata);
}
template <typename T>
bool Node<T>::operator==(const Node<T> &other)
{
return (data == other.data);
}
template <typename T>
bool Node<T>::operator<(const Node<T> &other)
{
return (data < other.data);
}
template <typename T>
bool Node<T>::operator>(const Node<T> &other)
{
return (data > other.data);
}
template <typename T>
bool Node<T>::operator<=(const Node<T> &other)
{
return (data < other.data || data == other.data);
}
template <typename T>
bool Node<T>::operator>=(const Node<T> &other)
{
return (data > other.data || data == other.data);
}
template <typename T>
bool Node<T>::operator!=(const Node<T> &other)
{
return (data != other.data);
}
template <typename T>
Node<T>::~Node()
{
NullPtrs();
}
template <typename T>
void Node<T>::SetData(const T &nodedata)
{
data = nodedata;
}
template <typename T>
void Node<T>::AddLeftChild(const Node<T> *leftchildptr)
{
leftchild = leftchildptr;
return;
}
template <typename T>
void Node<T>::AddRightChild(const Node<T> *rightchildptr)
{
rightchild = rightchildptr;
return;
}
template <typename U>
ostream& operator<<(ostream &out, Node<U> &node)
{
out << node.data;
return out;
}
template <typename T>
void Node<T>::Copy(const Node &other)
{
leftchild = other.leftchild;
rightchild = other.rightchild;
data = other.data;
}
#endif // NODE_H
Any help would be very much appreciated.
You're passing an rvalue (the constant 1) to Heap::AddToHeap(T &data), which takes a non-const reference. Change the function signature so it takes a const reference. You'll also have to propagate this to Heap<T>::AddToVacantNode(T &data) and any other relevant functions.
Here's an SO answer about const-correctness: Sell me on const correctness
You are calling AddToHeap with the literal 1. But your AddToHeap only accepts ints by reference. You can't pass the literal 1 by reference. If your AddToHeap function accepted the parameter by const reference, it would work.
void AddToHeap(const T& data);