Friday 15 April 2011

c++ - Doubly linked list and polymorphism -



c++ - Doubly linked list and polymorphism -

i'm trying figure out on how create doubly linked list work polymorphism, have no clue :( i'm trying implement classic illustration of animal , have failed. here code:

#include <iostream> #include<string> #include<cstdlib> #include<cstring> #include<sstream> using namespace std; // node class template<typename t> class nodetype { public: nodetype(); nodetype(t); ~nodetype(); nodetype *next; nodetype *previous; t data; string id; void imprime(); // getkey string getkey() { stringstream llave; llave << data.getkey(); llave >> id; homecoming id; } }; // defect template<typename t> nodetype<t>::nodetype() { info = null; next = null; previous = null; } // parameter template<typename t> nodetype<t>::nodetype(t data_) { info = data_; next = null; previous = null; } // node printing template<typename t> void nodetype<t>::imprime() { data.imprime(); } template<typename t> nodetype<t>::~nodetype() { delete next; delete previous; } // list template <class t> class list { private: nodetype<t> *first; nodetype<t> *last; nodetype<t> *current; int m_num_nodes; public: list(); ~list(); void add_head(t); void add_end(t); void add_sort(t); //void fill(); void search(string r); //void del_by_data(t); void del_by_data(int n); void print(); }; template<typename t> list<t>::list() { m_num_nodes = 0; first = null; lastly = null; } //add in origin ... template<typename t> void list<t>::add_head(t data_) { nodetype<t> *new_node = new nodetype<t>(data_); nodetype<t> *temp = first; if (!first) { first = new_node; } else { new_node->next = first; first = new_node; while (temp) { temp = temp->next; } } m_num_nodes++; } // add together lastly template<typename t> void list<t>::add_end(t data_) { nodetype<t> *new_node = new nodetype<t> (data_); nodetype<t> *temp = first; if (!first) { first = new_node; } else { while (temp->next != null) { temp = temp->next; } temp->next = new_node; } m_num_nodes++; } // supposed sorts items in list ... template<typename t> void list<t>::add_sort(t data_) { nodetype<t> *new_node = new nodetype<t> (data_); nodetype<t> *temp = first; if (!first) { first = new_node; lastly = new_node; } else { if (first->getkey() > new_node->getkey()) { new_node->next = first; first = new_node; } else { while ((temp->next != null) && (new_node->getkey() > temp->next->getkey())) { temp = temp->next; } // .. ¿? .. new_node->next = temp->next; temp->next = new_node; temp->previous = new_node->previous; new_node->previous = temp; temp->next = new_node; } } m_num_nodes++; } // print list template<typename t> void list<t>::print() { nodetype<t> *current; current = first; while (current != null) { current->imprime(); current = current->next; } } // ............................................ template<typename t> void list<t>::search(string r) { bool found = false; current = first; while (current != null && !found) { if (current->getkey() == r) { found = true; current->imprime(); } else { current = current->next; } } if (!found) cout << "element not found\n"; } // -------------------------------------------- template<typename t> void list<t>::del_by_data(int n) { nodetype<t> *temp = first; nodetype<t> *del; int i; bool deleted = false; if (n > 1 && n <= m_num_nodes) { for(i=1;i<n;i++) { temp=temp->next; } if (n == 1) { first = temp->next; temp->next->previous = null; } else if(n == m_num_nodes) { lastly = temp->previous; temp->previous->next = null; } else { temp->previous->next = temp->next; temp->next->previous = temp->previous; } } m_num_nodes--; } // destroy constructor template<typename t> list<t>::~list() { nodetype<t> *item; nodetype<t> *next; for(item=first; item != 0; item = next) { next = item->next; delete item; } } class animal { public: virtual void print()const { cout << "unknown animal type.\n"; } virtual ~animal(){} string getkey() { string tipoanimal; } void imprime() { cout << "ok" << endl; }; protected: int npatas; string tipoanimal; }; //... clase ave .. class ave: public animal { protected: string nombre; string nada; string tipo; string canta; string carroniero; public: ave(int n, string c, string nom, string tip, string cant, string car, string nad) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; canta = cant; carroniero = car; nada = nad; } void print()const { cout << "un " << nombre << " es united nations " << tipoanimal << " que tiene " << npatas << " patas y es del tipo " << tipo << " " << canta << " canta y " << carroniero << " es carroñero y " << nada << " nada" << endl; } }; // mamifero class mamifero : public animal { protected: string nombre; string tipo; public: mamifero(int n, string c, string nom, string tip) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; } void print() const { cout << "un " << nombre << " es united nations " << tipoanimal << " que tiene " << npatas << " patas y es del tipo " << tipo << endl; } }; // pez class pez : public animal { protected: string nombre; string tipo; string nace; // ovíparo, vivíparo public: pez(int n, string c, string nom, string tip, string nac) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; nace =nac; } void print() const { cout << "un " << nombre << " es united nations " << tipoanimal << " que tiene " << npatas << " patas y es del tipo " << tipo << ", además es " << nace << endl; } }; // reptil class reptil : public animal { protected: string nombre; string tipo; string venenoso; string tamanio; public: reptil(int n, string c, string nom, string tip, string ven, string tam) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; venenoso = ven; tamanio = tam; } void print() const { cout << "un " << nombre << " es united nations " << tipoanimal << " que tiene " << npatas << " patas y es del tipo " << tipo << " y " << venenoso << " venenoso, además es " << tamanio << endl; } }; // ... aves .. class aguila : public ave { public: aguila(int n, string c, string nom, string tip, string cant, string car, string nad) : ave(n,c,nom, tip, cant, car, nad) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; canta = cant; carroniero = car; nada = nad; } }; class colibri : public ave { public: colibri(int n, string c, string nom, string tip, string cant, string car, string nad) : ave(n, c, nom, tip, cant, car, nad) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; canta = cant; carroniero = car; nada = nad; } }; // ... mamíferos ... class gato : public mamifero { public: gato(int n, string c, string nom, string tip) : mamifero(n, c, nom, tip) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; } }; class elefante : public mamifero { public: elefante(int n, string c, string nom, string tip) : mamifero(n, c, nom, tip) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; } }; // ... peces ... class beta : public pez { public: beta(int n, string c, string nom, string tip, string nac) : pez(n, c, nom, tip, nac) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; nace = nac; } }; class angel : public pez { public: angel(int n, string c, string nom, string tip, string nac) : pez(n, c, nom, tip, nac) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; nace = nac; } }; // ... reptil ... class lagartija : public reptil { public: lagartija(int n, string c, string nom, string tip, string ven, string tam) : reptil(n, c, nom, tip, ven, tam) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; venenoso =ven; tamanio = tam; } }; class tortuga : public reptil { public: tortuga(int n, string c, string nom, string tip, string ven, string tam) : reptil(n, c, nom, tip, ven, tam) { npatas = n; tipoanimal = c; nombre = nom; tipo = tip; } }; // ... imprime ... void imprime(animal animal) { animal.print(); } // +++++++++++++++++++++++++ // +++++++++++++++++++++++++ int main() { list<animal> * list1 = new list<animal>(); string element1; string element2; // ...... int count = 10; //animal* p[count]; ave* b[count]; mamifero* m[count]; pez* pp[count]; reptil* r[count]; int i, j ; // ...... int dim, choice, pos; dim = 0; do{ cout << "select choice.\n"; cout << "1. print list\n"; cout << "2. delete element of list\n"; cout << "3. search element of list\n"; cout << "4. exit\n"; cout << "option: "; cin >> choice; cout << endl; switch(choice) { case 1: { cout << "printing list:\n"; cout << "se van añadir los siguientes elementos:\n"; cout << "animal: ave. patas: 2, tipo: aguila, alimento:carnivora," << "canta: no, carroñero: no, nada:no\n"; // ... agrega elementos la lista ... list1->add_sort(new ave(2,"ave","aguila", "carnívoro", "no", "no", "no")); list1->print(); cout<<endl; break; } case 2: { dim++; if (dim == 1) { cout << "element delete: \n"; list1->del_by_data(1); element1 = ""; cout << endl; list1->print(); cout << endl; cout << "\nelement deleted\n"; cout<<endl; } if (dim == 2) { cout << "element delete: \n"; list1->del_by_data(4); cout << endl; list1->print(); cout << endl; cout << "\nelement deleted\n"; cout << endl; dim = 0; } break; } case 3: { dim++; if (dim == 1) { cout << "element search: \n"; list1->search(""); element1 = ""; cout << endl; list1->print(); cout << endl; } if(dim == 2) { cout << "element search: \n"; list1->search(""); element1 = ""; cout << endl; list1->print(); cout << endl; dim = 0; } break; } } }while(choice != 4); homecoming 0; }

when seek compile code, error appears: error: no matching function phone call ‘list::add_sort(ave*)’ list1->add_sort(new ave(2,"ave","aguila", "carnívoro", "no", "no", "no"));

any can explain me error means , how prepare it?? (o.o) (?)

thanks in advance.

c++ polymorphism doubly-linked-list

No comments:

Post a Comment