2010-11-03 120 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); 
    } 
} 

希望这有助于。 干杯。