2016-11-05 65 views
1

我需要建立一個鏈接列表與模板,但我不知道爲什麼不工作,我已經建立鏈接列表之前,但從來沒有模板。現在我的問題是,如果我創建的列表中一切正常,但是當我嘗試插入的東西給它,我得到以下錯誤:鏈接列表模板

Error C2664 'Nodo<D>::Nodo(Nodo<D> &&)': cannot convert argument 1 from 'const int' to '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 from 'const int' to 'const Nodo<D> &' Datos2 d:\google drive\visual studio 2015\projects\datos2\datos2\listaSimple.h 73 

我下面的代碼:

//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; 
    } 
} 

//Source.cpp 
#include <iostream> 
#include <string> 
#include "linkedList.h" 


using namespace std; 

int main() { 
    listaSimple<int> Nueva; 
    Nueva.InsertarInicio(5); 

    system("pause"); 
    return 0; 
} 
+0

'if(EmptyList)'是實際的代碼嗎?因爲你似乎想調用這個函數,在這種情況下你會丟失括號,例如:'if(EmptyList())' – UnholySheep

+0

@UnholySheep yep,對不起,翻譯錯誤。謝謝指出它 – Twhite1195

+0

什麼是'Siguiente'?也沒有定義'D類'。'listaSimple NewList'全錯了,你可能是指'linkedList NewList',我的'D'是一些沒有顯示的類。 –

回答

1

NodelinkedList修正版本。請注意,NodelinkedList不包含有關實際數據的任何信息。事實上,你可以在最後申報數據(struct MyData)。

對於印刷我添加了一個功能:

node->data.print(); 

這樣NodelinkedList不用於打印數據直接負責,他們並不需要知道數據什麼。他們可以要求DataType打印數據。 DataType必須包含一個print函數來打印自己的內容。

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; //<== you forgot this 
    } 

    void Print() 
    { 
     Node<DataType> *walk = first; 
     while (walk) 
     { 
      walk->data.print(); 
      walk = walk->Next; 
     } 
    } 

private: 
    Node<DataType> *first; 
}; 

現在您可以聲明MyData並使用它。確保MyData包含一個print函數。由於數據被分配的方式,MyData也必須是POD(普通的舊數據,它不能包含指針)。

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; 
} 

另一種方法:

您可以添加<<運算符重載爲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; 
    } 
}; 

所以MyData知道如何打印自己。例如:

MyData data; 
data.ch = 'A'; 
data.binario = 1; 
data.charge = 10; 
cout << data << "\n"; 

這應打印"A, 1, 10"

然後你可以改變linkList::Print()

... 
void Print() 
{ 
    Node<DataType> *walk = first; 
    while (walk) 
    { 
     std::cout << walk->data << "\n"; 
     walk = walk->Next; 
    } 
} 

現在linkedList是獨立的MyData只要MyData具有<<運算符重載(和它的數據是POD)。您也可以將此鏈接列表用於基本類型。例如:

linkedList<int> test; 
test.InsertBegining(1); 
test.InsertBegining(2); 
test.Print(); 
+0

太棒了!但是我仍然有一些問題,每次我想要使用模板時,是否必須聲明一個結構? – Twhite1195

+0

這是一個非常基本的問題,考慮它是如何工作的,並嘗試在不同的地方申報,看看有什麼作用。你可以在全局範圍聲明'MyData'並在任何地方使用它。 –

+0

我想到了這一點,但由於模板讓我頭痛,所以我應該先問一下。無論如何,感謝您幫助我,我想我現在得到它的掛鉤 – Twhite1195