2013-02-11 120 views
0

我真的很新C++,並且無法使用insertList()函數來處理LinkedList。下面是我得到了,出發代碼:C++鏈接列表包含()函數

#include <iostream> 
#include <cassert> 
#include <stdexcept> 
using namespace std; 

template<typename T> class mylist; 

template<typename T> 
ostream& operator<< (ostream &out, const mylist<T> &l); 

template <typename T> 
class mylist { 
public: 
    // node definition 
    struct node { 
     T data; 
     node* next_ptr; 
     // node constructor 
     node(const T &d, node* n):data(d),next_ptr(n){} 
    }; 

    // alternative node definition 
    /* 
    class node { 
    public: 
     T data; 
     node* next_ptr; 
     // node constructor 
     node(const T&d, node* n):data(d),next_ptr(n){} 
    }; 
    */ 

    // linked list head pointer 
    node* head_ptr; 

    //friend ostream& operator<< <>(ostream& out, const mylist<T>&l); 
    friend ostream& operator<< (ostream &out, const mylist<T> &l); 

public: 
    // default constructor 
    mylist():head_ptr(nullptr) {} // head_ptr points nowhere 

    // adds element to the front of the linked list 
    void push_front(const T &elem) { 
     head_ptr = new node(elem, head_ptr); 
    } 

    // check if linked list is empty 
    bool empty() { return head_ptr == nullptr;} 

    // number of nodes in linked list 
    unsigned size() { return length();} 
    unsigned length() { 
     unsigned l = 0; 
     for(node* current = head_ptr; current != nullptr; current = current->next_ptr) { 
      ++l; 
     } 
     return l; 
    } 

    // copy constructor 
    mylist(const mylist &other) 
    { 
     for(node* current_other = other.head_ptr; 
      current_other != nullptr; 
      current_other = current_other->next_ptr) { 
       this.push_back(current_other->data); // inefficient, but easy :) 
     } 
    } 

    // destructor 
    ~mylist() { 
     node* tmp; 
     for(node* current = head_ptr; 
       current != nullptr; 
       current = tmp) { 
      tmp=current->next_ptr; 
      delete current; 
     } 
    } 

    // at accessor method (returns the element at the ith position in the linked list) 
    T& at(unsigned i){ 
     unsigned l=0; 
     node* current; 
     for(current = head_ptr; current != nullptr; current = current->next_ptr) { 
      if(l == i) 
       break; 
      ++l; 
     } 
     if (current == nullptr) 
      throw out_of_range("index i is out of range"); 
     else 
      return current->data; 
    } 

    // bracket operator (returns the element at the ith position in the linked list) 
    T& operator[](unsigned i){ 
     return at(i); 
    } 

    // adds element to the end of the linked list 
    void push_back(const T &elem) { 
     if(empty()) { 
      push_front(elem); 
      return; 
     } 
     node* last_ptr; 
     for(last_ptr = head_ptr; last_ptr->next_ptr != nullptr; 
      last_ptr = last_ptr->next_ptr); 

     last_ptr->next_ptr = new node(elem, nullptr); 

    } 

    // prints the contents of the linked list 
    void print_all(void) { 
     cout << "mylist{"; 
     for(node* current_ptr = head_ptr; 
       current_ptr != nullptr; 
       current_ptr = current_ptr->next_ptr){ 
      cout << current_ptr->data << " "; 
     } 
     cout << "}" << endl; 
    } 

我想創建一個新的功能,插入(常量牛逼& ELEM,無符號的我)。它的宗旨是在下面的代碼的註釋中描述:

// inserts the element at position i in linked list. 
    // throws out of range error if position i not in list. 
    void insert (const T &elem, unsigned i) { 
     unsigned l=0; 
     node* current, prev; 
     for(current = head_ptr; current != nullptr; current = current->next_ptr) { 

      if(l == i) 
       break; 
      ++l; 
      prev = current; 
     } 
     if (current == nullptr) 
      throw out_of_range("index i is out of range"); 
     else 
     { 
      prev->next_ptr = new Node (elem, current); 
     } 
    } 

我的問題是,我碰到下面的錯誤,我不知道如何解決它,或者是什麼意思:

1>c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_tpt.h(184): error C2512: 'mylist<T>::node' : no appropriate default constructor available 
1>   with 
1>   [ 
1>    T=std::string 
1>   ] 
1>   c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_tpt.h(182) : while compiling class template member function 'void mylist<T>::insert(const T &,unsigned int)' 
1>   with 
1>   [ 
1>    T=std::string 
1>   ] 
1>   c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_main.cpp(20) : see reference to class template instantiation 'mylist<T>' being compiled 
1>   with 
1>   [ 
1>    T=std::string 
1>   ] 

預先感謝您提供的任何幫助!

+2

此錯誤無關與問題標題。 – 2013-02-11 09:10:24

+0

[可能的重複](http://stackoverflow.com/search?q=cannot+convert+%27this%27+pointer+from) – 2013-02-11 09:11:42

+2

再次閱讀錯誤,尤其是文件名和行號。 – 2013-02-11 09:12:22

回答

1

嘗試將const cualifier添加到empty功能:

bool empty() const { return head_ptr == nullptr;} 
+0

你的想法完美無缺!我現在無法使用我的插入功能......我已將我的問題中的代碼更改爲新代碼。 – 2013-02-11 10:28:16

+0

正如@JoachimPileborg所述,錯誤消息包含您需要修復錯誤的所有信息。是的,我不得不承認,這些錯誤信息很難閱讀,但多練習一下,你會習慣於閱讀它。關於你的新問題,我想(因爲你還沒有發佈行號)需要你的節點類的默認構造函數(可能在複製你的自定義列表時),但是,由於我認爲這個問題的主題發生了變化最好是開一個新的問題。 – 2013-02-11 11:00:18