2010-11-03 121 views
1

我得到一個編譯錯誤,我使用插入函數的創建函數。當我在if子句中執行不同的調用時,它可以工作,但是我想將它移動到單獨的創建函數中。任何關於我得到的編譯錯誤的幫助表示讚賞C++鏈接列表插入函數

| 76 |錯誤:無法轉換list_link*' to sorted_list :: list_link *'in assignment |

頭文件

class sorted_list 
{ 
public: 
    sorted_list(); // Constructor declaration 
    ~sorted_list(); // Destructor declaration 

    void insert(int key, double value); 
    void print(); 


private: 
    class list_link 
    { 
     // Declarations 
     public: 
     my_key_type key; 
     my_value_type value; 
     list_link *next; 
    }; 
    list_link* first; 

}; 

功能

void sorted_list::insert(int key, double value) 
{ 
    list_link *curr, *prev; 

    curr = first; 

    while(curr) 
    { 
     prev = curr; 
     curr = curr->next; 
    } 
    if(first == 0 || prev == 0) //if empty or add first 
    { 
     cout << "empty list" << endl; 
     list_link *new_link = new list_link; 
     new_link->key = key; 
     new_link->value = value; 
     new_link->next = 0; 
     first = new_link; 

    } 
    else 
    { 
     cout << "add" << endl; 
     prev->next = create(key, value, 0); 
    } 
} 

創建函數

list_link* create(my_key_type key, my_value_type value, list_link* next) 
{ 
    // creates the node; 
    list_link *new_link = new list_link; 

    // add values to the node; 
    new_link->key = key; 
    new_link->value = value; 
    new_link->next = next; 

    return new_link; 
} 

回答

1

list_link是:

  1. 宣佈在sorted_list
  2. 標記爲private

爲了有一個獨立的功能來創建此類型的對象,你需要做類型的公共範圍,你也將需要將前綴它與sorted_list::,或者你需要聲明它在sorted_list類之外。我應該補充一點,你使用list_link作爲一個簡單的數據對象,沒有方法且字段是公開的,所以 - 從純粹的文體角度來看 - 我建議將它聲明爲struct而不是類,這也是消除了對公衆的需求。

+0

有沒有其他的方法,因爲我想保持創建節點內部的類只有 – starcorn 2010-11-03 01:15:31

+2

@starcorn,如果它是內部的,那麼不要使用獨立函數;使create函數成爲sorted_list的一個私有函數。 – 2010-11-03 02:33:25

+0

啊,這就是我的想法,但在我看到我不得不在.cc文件中編寫sorted_list :: list_link sorted_list :: create(){}之前,我一直堅持使用它。無論如何,我想我現在整理出來了。謝謝 – starcorn 2010-11-03 09:28:35

0

我不是C++的權威人士,但我認爲這個問題伴隨着你正在調整的東西。

  1. list_link類是私人的。我建議這是公開的,因爲類只是通過其創建對象實例的藍圖。你可以保持私有的是指向鏈表的實際指針,list_link *first

  2. 由於list_link類的sorted_list類下築巢,你必須通過sorted_list範圍內的每個嘗試訪問list_link上課時間去。

嘗試了這一點的解決辦法如下:

class sorted_list 
{ 
public: 
    sorted_list(); // Constructor declaration 
    ~sorted_list(); // Destructor declaration 

    void insert(int key, double value); 
    void print(); 


    class list_link 
    { 
     // Declarations 
     public: 
     my_key_type key; 
     my_value_type value; 
     list_link *next; 
    }; 
private: 
    list_link* first; 

}; 

sorted_list::list_link* create(my_key_type key, my_value_type value, sorted_list::list_link* next) 
{ 
    // creates the node; 
    sorted_list::list_link *new_link = new sorted_list::list_link; 

    // add values to the node; 
    new_link->key = key; 
    new_link->value = value; 
    new_link->next = next; 

    return new_link; 
} 

void sorted_list::insert(int key, double value) 
{ 
    list_link *curr, *prev; 

    curr = first; 

    while(curr) 
    { 
     prev = curr; 
     curr = curr->next; 
    } 
    if(first == 0 || prev == 0) //if empty or add first 
    { 
     cout << "empty list" << endl; 
     list_link *new_link = new list_link; 
     new_link->key = key; 
     new_link->value = value; 
     new_link->next = 0; 
     first = new_link; 

    } 
    else 
    { 
     cout << "add" << endl; 
     prev->next = create(key, value, 0); 
    } 
} 

希望這有助於。 乾杯。