I get this error message while I'm trying to use copy constructor, I understand why the error message is showing but I don't know how to resolve it.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <ctime>
#include <cmath>
#include <vector>
#include <cassert>
using namespace std;
class Error{
private:
string err;
public:
Error(string s){err = s;}
string get() const{return err;}
};
class QueueFull:public Error{
public:
QueueFull(string e):Error(e){}
};
class QueueEmpty:public Error{
public:
QueueEmpty(string e):Error(e){}
};
template<typename Object>
class SingleList{
protected:
class Node;
typedef Node* NodePtr;
private:
NodePtr last;
NodePtr first;
int sz;
void free();
void copy(const SingleList<Object>& s);
public:
class Iterator;
SingleList(){sz=0;last=NULL;first=NULL;}
~SingleList();
SingleList(const SingleList<Object>& s);
SingleList<Object>& operator=(const SingleList<Object>& s);
void push(Object a);
void pop()throw (QueueEmpty);
int size() const{return sz;}
Iterator begin();
Iterator end();
};
template<typename Object>
class SingleList<Object>::Node{
private:
Object element;
NodePtr next;
friend class SingleList<Object>;
friend class SingleList<Object>::Iterator;
public:
Node(const Object& e = Object(),Node* n = NULL):element(e),next(n){}
};
template<typename Object>
class SingleList<Object>::Iterator{
private:
NodePtr pos;
NodePtr last;
public:
Iterator(){pos=NULL;last=NULL;}
Object operator*() const;
void operator++(int dummy);
bool operator==(Iterator i)const;
bool operator!=(Iterator i)const;
friend class SingleList<Object>;
};
template<typename Object>
Object SingleList<Object>::Iterator::operator*() const
{
assert(pos !=NULL);
return pos->element;
}
template<typename Object>
void SingleList<Object>::Iterator::operator++(int dummy)
{
assert(pos != NULL);
pos = pos->next;
}
template<typename Object>
bool SingleList<Object>::Iterator::operator==(Iterator i) const
{
return pos == i.pos;
}
template<typename Object>
bool SingleList<Object>::Iterator::operator!=(Iterator i) const
{
return pos != i.pos;
}
template<typename Object>
typename SingleList<Object>::Iterator SingleList<Object>::begin()
{
Iterator iter;
iter.pos = first;
iter.last = last;
return iter;
}
template<typename Object>
typename SingleList<Object>::Iterator SingleList<Object>::end()
{
Iterator iter;
iter.pos = NULL;
iter.last = last;
return iter;
}
template<typename Object>
void SingleList<Object>::copy(const SingleList<Object>& s)
{
for( Iterator pos = s.begin();pos != s.end();pos++)
push(*pos);
}
template<typename Object>
void SingleList<Object>::free()
{
while(sz != 0)
pop();
}
template<typename Object>
SingleList<Object>::~SingleList()
{
free();
}
template<typename Object>
SingleList<Object>::SingleList(const SingleList<Object>& s)
{
first = NULL;
last = NULL;
copy(s);
}
template<typename Object>
SingleList<Object>& SingleList<Object>::operator =(const SingleList<Object>& s)
{
free();
copy(s);
return *this;
}
template<typename Object>
void SingleList<Object>::push(Object a)
{
Node* n = new Node(a);
if (last == NULL)
{
last = n;
first = n;
}
else
{
last->next = n;
last = n;
last->next = NULL;
}
sz++;
}
template<typename Object>
void SingleList<Object>::pop()
{
if (size() == 0)
throw QueueEmpty("Empty Queue.\n");
else
{
Node* del = first;
first = first->next;
delete del;
sz--;
}
}
int main()
{
int j = 0;
while(j == 0)
{
SingleList<int> l;
SingleList<int> b(l);
l.push(1);
l.push(2);
l.push(3);
//cout << l.size();
SingleList<int>::Iterator iter;
iter = l.begin();
for(;iter != l.end();iter++ )
cout << *iter;
j++;
}
return 0;
}