我得到一个编译错误,我使用插入函数的创建函数。当我在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;
}
有没有其他的方法,因为我想保持创建节点内部的类只有 – starcorn 2010-11-03 01:15:31
@starcorn,如果它是内部的,那么不要使用独立函数;使create函数成为sorted_list的一个私有函数。 – 2010-11-03 02:33:25
啊,这就是我的想法,但在我看到我不得不在.cc文件中编写sorted_list :: list_link sorted_list :: create(){}之前,我一直坚持使用它。无论如何,我想我现在整理出来了。谢谢 – starcorn 2010-11-03 09:28:35