#include <iostream>
#include <cstdlib>
#include <assert.h>
using namespace std;
template <class T> class link;
template <class T> class Iterator;
template <class T> class Ring;
class term;
template <class T> class list
{
protected:
link <T> *First; //data field
public:
list() : First(NULL) {} //konstruktor domyslny (bez agrumentow)
list(const int &source); //konstruktor kopiujacy
//virtual ~list(); //destruktor
virtual void add(T value);
virtual void delete_all();
T first_element() const; //dostep do pierwszego elementu
virtual bool includes(T value) const; //inclusion test
virtual bool is_empty() const;
virtual void remove_first();
friend class Iterator <T>;
};
template <class T> class link
{
public:
link <T> * insert(T value); //after current link
private:
link(T val,link *ptr) : value(val),Next(ptr){}
link<T> *Next;
T value;
friend class list <T>;
friend class Ring <T>;
friend class Iterator <T>;
};
template <class T> link <T> * link <T> :: insert(T value)
{
Next=new link <T>(value,Next);
assert(Next!=NULL);
return Next;
}
template <class T> void list <T> :: add(T value)
{
First=new link <T>(value,First);
assert(First!=NULL);
}
template <class T> T list <T> :: first_element() const
{
assert(First != NULL);
return First->value;
}
template <class T> bool list <T> :: is_empty() const
{
return First==NULL;
}
template <class T> bool list <T> :: includes(T value) const
{
for(link <T> *p = First; p; p = p->Next)
{
if(value == p->value)
return true;
}
return false;
}
template <class T> void list <T> :: remove_first()
{
assert(First != NULL);
link <T> *ptr = First; //save pointer to the first item
First = ptr->Next; //reasign the First node
delete ptr;
}
template <class T> void list <T> :: delete_all()
{
link <T> *next;
for(link <T> *p = First;p ;p = next) //p - pomocniczy wskaznik sterujacy w petli
{
next = p->Next;
delete p;
}
First = NULL;
}
template <class T> class Iterator
{
public:
Iterator(list <T> &aList);
virtual bool init(); // ustaw iterator na poczatek listy
virtual T operator()(); //przeladowanie operatora ()
virtual bool operator !();
virtual bool operator++(); //for prefix, for postfix add dummy int
virtual void operator=(T value);
void remove_current();
void add_before(T new_value); // wstawianie elementow przed iteratorem
void add_after(T new_value); // wstawianie elementow po iteratorze
protected:
list <T> &my_list; // data fields, & przed obiektem oznacza przezwisko, referencje
link <T> *previous; // do juz istniejacego obiektu
link <T> *current;
};
template <class T> Iterator <T> :: Iterator(list <T> &aList) : my_list(aList)
{
init();
}
template <class T> bool Iterator <T> :: init()
{
previous = NULL;
current = my_list.First;
return current != NULL;
}
template <class T> T Iterator <T> :: operator()()
{
assert(current != NULL);
return current->value;
}
template <class T> void Iterator <T> :: operator=(T val)
{
assert(current != NULL);
current->value = val;
}
template <class T> void Iterator <T> :: remove_current()
{
assert(current != NULL);
if (previous == NULL)
my_list.First = current->Next;
else
previous->Next = current->Next;
delete current;
current = NULL;
}
template <class T> bool Iterator <T> :: operator++()
{
if (current == NULL) //do juz istniejacego obiektu
{
if (previous == NULL) // to next element
current = my_list.First; // kwestia umowna
else
current = previous->Next; //odtwarzam wskaznik current
}
else
{
previous = current;
current = current->Next;
}
return current != NULL;
} //valid for prefix operator only
template <class T> bool Iterator <T> :: operator !()
{
if (current == NULL && previous != NULL)
current = previous->Next;
return current != NULL; //termination of Iterator
}
template <class T> void Iterator <T> :: add_before(T val) //Wstawianie na liste przed current
{
if (previous)
previous = previous->insert(val);
else
{
my_list.list<T>::add(val); // to avoid subclass
previous = my_list.First;
current = previous->Next; //if current is NULL
}
}
template <class T> void Iterator <T> :: add_after(T val) //Wstawianie na liste za current
{
if (current)
current->insert(val);
else if (previous)
current = previous->insert(val);
else
my_list.list<T>::add(val);
}
template <class T> class ordered_list : public list <T>
{
public:
virtual void add(T value);
};
template <class T> void ordered_list <T> :: add(T value)
{
Iterator <T> itr(*this);
for(itr.init(); !itr; ++itr)
{
if(value < itr())
{
itr.add_before(value);
return;
}
}
itr.add_before(value);
}
template <class T> class double_ended : public list <T>
{
public:
double_ended() : Last(NULL){}
virtual void add(T value);
virtual void delete_all();
virtual void remove_first();
void add_to_end(T value);
protected:
link <T> *Last;
};
template <class T> void double_ended <T> :: add(T value)
{
if(this->is_empty())
{
list <T> :: add(value);
Last=this->First;
}
else
list <T> :: add(value);
}
template <class T> void double_ended <T> :: add_to_end(T value)
{
if(Last!=NULL)
Last=Last->insert(value);
else
add(value);
}
template <class T> void double_ended <T> :: delete_all()
{
list <T> :: delete_all();
Last=NULL;
}
template <class T> void double_ended <T> :: remove_first()
{
list <T> :: remove_first();
if(this->is_empty())
Last=NULL;
}
template <class T> class Ring : public list <T>
{
public:
Ring(int max);
T dequeue(); // usun glowe kolejki
void enqueue(T Value); // wstaw nowy element do kolejki
T is_empty();
protected:
link <T> *Last_free;
link <T> *Last_filled;
};
template <class T> T Ring <T> :: dequeue()
{ // delete from a queue
assert(! this->is_empty());
Last_free = Last_free->Next;
return Last_free->value;
}
template <class T> void Ring <T> :: enqueue(T Value)
{ // insert a new object into a queue
if (Last_filled->Next == Last_free) // create a new link object
Last_filled = Last_filled->insert(Value);
else
{ //re-use old link objects
Last_filled = Last_filled->Next;
Last_filled->value = Value;
}
}
template <class T> Ring <T> :: Ring(int max)
{
T initial_value;
// create the first link
Last_free = new link <T>(initial_value,0);
assert (Last_free != NULL);
Last_filled = Last_free;
// create a loop by pointing to itself
Last_filled->Next = Last_filled;
// now add the remaining elements
while (max-- > 0)
Last_filled->insert(initial_value);
}
template <class T> T Ring <T> :: is_empty()
{
return Last_filled==Last_free;
}
class term
{
public:
term (int coef, int pow):coefficient(coef), exponent(pow){} // constructor
int coefficient; // data fields
/*const*/ int exponent; // once created never changes
};
term operator*(const term & left, const term & right)
{
term result (left.coefficient * right.coefficient, left.exponent + right.exponent);
return result;
}
term operator+(const term & left, const term & right)
{
assert(left.exponent == right.exponent);
term result (left.coefficient + right.coefficient, left.exponent);
return result;
}
bool operator== (const term & left, const term & right)
{
return ((left.coefficient == right.coefficient)
&& (left.exponent == right.exponent));
}
class polynomial
{
public:
polynomial(){}
polynomial(const polynomial & p)
{
} // copy constructor
polynomial(term & t)
{
terms.add(t);
}
// polynomial(int zero)
//{
// terms.add(new term(zero,0));
//}
//polynomial(int one, int zero)
//{
//}
void operator=(const polynomial & right);
void operator+=(const polynomial & right);
//void operator*=(const term &);
private:
list <term> terms; // data field
// friend polynomial operator*(const polynomial &,const polynomial &);
friend polynomial operator+(const polynomial &,const polynomial &);
};
void polynomial :: operator+=(const polynomial & right)
{
Iterator <term> itrL(terms);
Iterator <term> itrR(right.terms);
for (itrL.init(),itrR.init(); !itrL && !itrR; ++itrL)
{
while (!itrR && itrL().exponent < itrR().exponent)
{
itrL.add_before(itrR());
++itrR;
}
if (!itrR && itrR().exponent == itrL().exponent)
{
itrL = itrL() + itrR();
++itrR;
}
}
while (!itrR)
{
itrL.add_before(itrR());
++itrR;
}
}
polynomial operator+(const polynomial & left,const polynomial & right)
{// duplicate left using the copy constructor
polynomial result(left);
// use already defined operator +=
result += right;
return result;
}
int main()
{
return 0;
}
Klasy list i Iterator wcześniej sam zdefinowałem (jako szablony) i one są w porządku, bo sprawdzałem, natomiast program się nie kompiluje w tej linijce:
Iterator <term> itrR(right.terms);
Chciałbym wiedzieć dlaczego się nie kompiluje. Nie interesujcie się tym co program ma robić, tylko tym dlaczego mi wywala błąd.
error: no matching function for call to 'Iterator<term>::Iterator(const list<term>&)'|
Nie mogę chiny tego ogarnąć.