2013-12-21 219 views
1

我正在用C++創建一個蛇的遊戲,但我在使用我創建的linkedList類時遇到了一些麻煩。當我用Python製作蛇時,我在列表中列出了列表來表示組成蛇的每個圓的x和y位置。我正在嘗試在C++中做類似的事情。這裏是我的模板化的LinkedList類我做了,如果由於某種原因,你需要看到它:如何在鏈接列表中創建鏈接列表?

#ifndef LLIST_H_INCLUDED 
#define LLIST_H_INCLUDED 
#include <cstddef> 
#include <iostream> 

using namespace std; 

template <class T> 
class linkedList 
{ 
public: 
    class node 
    { 
    public: 
     ///node class attributes 
     T mPayload; 
     node* mNext; 
     ///constructor 
     node(T toucan):mPayload(toucan),mNext(NULL) 
     {} 
     ///destructor 
     ~node() 
     { 
      ///cascading delete 
      if(mNext) 
       delete mNext; 
     } 
     ///node class methods 
    }; 

    ///linkedList class attributes 
    node* mStart; 
    ///constructor 
    linkedList():mStart(NULL) 
    {} 
    ///destructor 
    ~linkedList() 
    { 
     ///initializes the cascading delete. 
     if(mStart) 
      delete mStart; 
    } 
    ///linkedList class methods 
    T mReturnT(int indx) 
    { 
     if(!mStart) 
      return NULL; 
     else 
     { 
      node* cur; 
      for(int i = 0; i<indx; i++) 
      { 
       if(!cur->mNext) 
       { 
        cout << "Indx out of range. Deleting last item." << endl; 
        break; 
       } 
       cur = cur->mNext; 
      } 
      delete cur; 
      return cur->mPayload; 
     } 
    } 

    void mInsert(int indx, T data) 
    { 
     ///Insert an item at a given position. 
     ///The first argument is the index of 
     ///the element before which to insert. 
     node* cur = mStart; 
     for(int i = 0; i < indx; i++) 
     { 
      if(!cur->mNext) 
       break; 
      else 
      { 
       cur = cur->mNext; 
      } 
     } 
     node* N = new node(data); 
     node* temp = cur->mNext; 
     cur->mNext = N; 
     N->mNext = temp; 
    } 

    T mPop() 
    { 
     ///Removes the last item in the list, 
     ///and returns it. 
     if(!mStart) 
      return NULL; 
     else 
     { 
      node* cur = mStart; 
      while(cur->mNext) 
      { 
       cur = cur->mNext; 
      } 
      T var = cur->mPayload; 
      delete cur; 
      return var; 
     } 
    } 

    int mSize() 
    { 
     if(!mStart) 
      return 0; 
     else 
     { 
      node* cur = mStart; 
      int counter = 1; 
      while(cur->mNext) 
      { 
       cur = cur->mNext; 
       counter++; 
      } 
      delete cur; 
      return counter; 
     } 
    } 
    void mPrint() 
    { 
     ///prints all values in a list. 
     node* cur; 
     if(!mStart) 
      cout << "List is empty." << endl; 
     else 
     { 
      cur = mStart; 
      cout << "["; 
      while(cur) 
      { 
       cout << cur->mPayload << " "; 
       cur = cur->mNext; 
      } 
      cout << "]" << endl; 
      delete cur; 
     } 
    } 
    void swapNodes(node* N) 
    { 
     ///idk 
    } 
    void bubbleSort() 
    { 
     if(!mStart) 
      return; 
     node* cur = mStart; 
     while(cur) 
     { 
      cout << cur->mPayload << endl; 
      if(cur->mRight->mFrequency > cur->mFrequency) 
      { 
       swapNodes(cur); 
       cur = mStart; 
      } 
      else 
       cur = cur->mRight; 
     } 
     delete cur; 
    } 

}; 

#endif // LLIST_H_INCLUDED 

現在在我的main.cpp中,我願做這樣的事情,讓我有鏈表的鏈表:

linkedList<linkedList> p1Snake; 
linkedList<int> startingPiece; 
startingPiece.mInsert(0,600); //This is the starting y position of 
         // p1snake added to the front of the list. 
startingPiece.mInsert(0,350); //This is the starting x position of 
         //p1snake added to the front of the list. 
p1Snake.mInsert(0,startingPiece); 

我的問題出現在該代碼的第一行。錯誤:模板參數列表中的模板類class linkedList的參數1處的類型/值不匹配。我該如何解決這個問題?

+0

我服用swapNodes和冒泡方法超出我的鏈接列表類。所以現在,他們可以被忽略。 – DebrisHauler

回答

1

lnkedList類有1個模板化的類型T,所以變量定義的語法應該是:

linkedList<type> variableName; 

遞歸,type可以是linkedList,但它應該仍然在上面的形式(有type)。 因此,例如,如果最後的數據類型爲int

linkedList<linkedList<int> > variableName; 
1

這需要成爲東西鏈表的鏈表:

linkedList<linkedList> p1Snake; 

這些方針的東西:

linkedList< linkedList<something> > p1Snake;