1

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*' tosorted_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;
}

2 Answers 2

1

The class list_link is:

  1. Declared in the scope of sorted_list
  2. Marked as 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.

Sign up to request clarification or add additional context in comments.

3 Comments

is there's some other way, since I want to keep the Node creation internal to the class only
@starcorn, if it is internal, then don't use a freestanding function; make the create function a private function of sorted_list.
ah, that's what I thought but I kept struggeling with it for quite some time before I saw that I had to write sorted_list::list_link sorted_list::create() {} in the .cc file. Anyway I think I sorted it out now. Thanks
0

I am not an authority in C++ but I think the problem comes with the way you are scpping things.

  1. 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.

  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.