Reputation: 8551
I get a compile error for a create function I use with the insert function. It works though when I do the different calls in the if clause, but I want to move it to a seperate create function instead. Any help regarding the compile error I get is appreciated
|76|error: cannot convert list_link*' to
sorted_list::list_link*' in assignment|
The header file
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;
};
The functions
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);
}
}
create function
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;
}
Upvotes: 1
Views: 6234
Reputation: 6082
I am not an authority in C++ but I think the problem comes with the way you are scpping things.
The list_link
class is private. I recommend this being public since a class is only a blueprint through which object instances can be created. What you can keep private is the actual pointer to the linked list, list_link *first
.
Since the list_link
class is nested under the sorted_list
class, you have to go through the sorted_list
scope each time you try to access the list_link
class.
Try this out for a fix:
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);
}
}
Hope this helps. Cheers.
Upvotes: 0
Reputation: 95639
The class list_link
is:
sorted_list
private
In order to have a freestanding function create an object of this type, you will need to make the type public, and you will also need to either prefix it with sorted_list::
, or you wil need to declare it outside of the sorted_list
class. I should add that you use list_link
as a simple data object, where there are no methods and the fields are public, and so -- from a purely stylistic perspective -- I would recommend declaring it as a struct
instead of a class, which also removes the need for public.
Upvotes: 1