c++ - Linked list with Template -


i need build linked list template, have no idea why not working, i've built linked lists before, never templates. right problem that, if create list ok, when try insert it, following errors:

error   c2664   'nodo<d>::nodo(nodo<d> &&)': cannot convert argument 1 'const int' 'const nodo<d> &'    datos2  d:\google drive\visual studio 2015\projects\datos2\datos2\listasimple.h 69   error   c2664   'nodo<d>::nodo(nodo<d> &&)': cannot convert argument 1 'const int' 'const nodo<d> &'    datos2  d:\google drive\visual studio 2015\projects\datos2\datos2\listasimple.h 73 

with following code:

    //linkedlist.h #pragma once #ifndef _listasimple_h #define _listasimple_h  template<class d>  struct nodo {     int carga;     int binario;      d caracter;      nodo<d> *siguiente;//means next };    template<class d> class listasimple {  public:     listasimple();     ~listasimple();      void insertarinicio(const d&);     bool listavacia();     void mostrar();     private:     nodo<d> *primero;     nodo<d> *ultimo;  };  template<class d>  listasimple<d>::listasimple() {     primero = null; }  template<class d> listasimple<d>::~listasimple() {     nodo<d> *aux;     while (primero != null)     {         aux = primero;         primero = primero->siguiente;         delete aux;     } }  template<class d> void listasimple<d>::insertarinicio(const d& dato) {     if (listavacia())     {         primero = new nodo<d>(dato);     }     else     {         nodo<d> *nodonuevo = new nodo<d>(dato);         nodonuevo->siguiente = primero;         primero = nodonuevo;     } }  template<class d> bool listasimple<d>::listavacia() {     if (primero == null)     {         return true;     }     else     {         return false;     } }  template<class d> inline void listasimple<d>::mostrar() {     nodo<d> *aux = primero;     while (aux != null)     {         cout << aux->caracter << "->";         aux = aux->siguiente;     } } 

and

//source.cpp #include <iostream> #include <string> #include "linkedlist.h"   using namespace std;  int main() {     listasimple<int> nueva;     nueva.insertarinicio(5);      system("pause");     return 0; } 

see corrected version of node , linkedlist. note node , linkedlist not contain information actual data. in fact can declare data (struct mydata) @ end.

for printing added function to:

node->data.print(); 

this way node , linkedlist not directly responsible printing data, , don't need know data. can ask datatype print data. datatype must contain print function print own content.

template<typename datatype> struct node {     datatype data;     node<datatype> *next;     node()     {         next = nullptr;     } };  template<typename datatype> class linkedlist { public:     linkedlist()     {         first = null;     }      ~linkedlist()     {         node<datatype> *aux;         while (first != null)         {             aux = first;             first = first->next;             delete aux;         }     }      void insertbegining(const datatype& data)     {         node<datatype> *newnode = new node<datatype>;         newnode->data = data;         if (first)         {             newnode->next = first;             first = newnode;         }          first = newnode; //<== forgot     }      void print()     {         node<datatype> *walk = first;         while (walk)         {             walk->data.print();             walk = walk->next;         }     }  private:     node<datatype> *first; }; 

now can declare mydata , use it. make sure mydata includes print function. mydata has pod (plain old data, can't contain pointers) because of way data being assigned.

int main()  {     struct mydata     {         int charge;         int binario;         char ch;         void print()         {             cout << charge << ", " << binario << ", " << ch << "\n";         }     };      linkedlist<mydata> list;     mydata data;      data.binario = 1;     data.ch = 'a';     data.charge = 10;     list.insertbegining(data);      data.binario = 2;     data.ch = 'b';     data.charge = 20;     list.insertbegining(data);      list.print();      system("pause");     return 0; } 

another method:

you can add << operator overload mydata

struct mydata {     int charge;     int binario;     char ch;      friend std::ostream& operator<< (std::ostream &out, mydata &x)     {         out << x.ch << ", " << x.binario << ", " << x.charge;         return out;     } }; 

so mydata knows how print itself. example:

mydata data; data.ch = 'a'; data.binario = 1; data.charge = 10; cout << data << "\n"; 

this should print "a, 1, 10".

then can change linklist::print()

... void print() {     node<datatype> *walk = first;     while (walk)     {         std::cout << walk->data << "\n";         walk = walk->next;     } } 

now linkedlist independent of mydata long mydata has << operator overload (and data pod). can use linked list fundamental types. example:

linkedlist<int> test; test.insertbegining(1); test.insertbegining(2); test.print(); 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -