Working on a C++ class function that has to return a pointer to a new_node. The function is struct node* List::createNode(int i){
struct node* new_node = new struct node;
new_node->data = i;
return new_node;
}
Class is "List", function defined as struct node* createNode(int i);, and I get this error cannot convert List::node* to node* in return
Node was defined in List.h, and this function is in List.cpp (essentially a class for basic linked list stuff).
My last experience was just creating something like this without using classes, so I'm pretty new to this and not sure what I should be doing here. Fruit loops for any input.
Name:
Anonymous2010-04-28 14:52
If you're defining the node inside List, you need to write List::node instead of just node.
I don't know what could be wrong, not enough information.
Name:
Anonymous2010-04-28 15:21
I tried this:
struct List::node* createNode(int i)
{
struct List::node* new_node = new struct List::node;
new_node->data = img;
return new_node;
}
Don't know if this is correct but the compiler isn't complaining.
I originally had my struct node business under private in List.h but it threw some errors about it not being public. Moved it under public and it went through. Looks a bit... idk, it's just not clicking yet but I still have more to stumble through.
The reason you can use just "node" in the function body but you must use "List::node" in the return type is that in the return type it hasn't encountered the function name yet, and thus it doesn't know that the function is going to be a member function in the class "List", so it just looks up "node" in the enclosing scope (global scope, probably).
Name:
Anonymous2010-04-28 19:15
>>4
That should have been obvious to me... the node was defined inside the class, so it needed the class name:: to be used, but this itself was a problem. I should not have defined the node inside the class.
Moved the node outside the class, works as it was written before. I really need to slow down and research more.
In my main, I have a function that creates the new int array, puts it in a new intobject, puts that intobject in a new node, and then places that node in the list. //intList was passed as List &intList, and new_array was created within this function
struct node* new_node;
intobject new_int;
new_int.set_array(new_array);
new_node = intList.create_node(new_int);
intList.insert_node(new_node, NULL);
return 0;
The error comes directly after return 0, when it's destructing the new_int (I put couts in the destructors - dump right after the intobject is deleted). Puzzled.
Oh hell, I just noticed that somewhere, that int array inside the new_int object gets deleted, or rather, is destructed when it shouldn't be.
Actually, it's destructed where it should be, but since I'm using/copying pointers, those pointers are now pointing to nothing, and thus cannot be deleted. It was both memory corruption and double free.
Changing my intobject's destructor to not delete the pointer seems to have fixed this problem, though it means a little more work to ensure there's no memory leak when I start needing to delete nodes.
Following the nodes lead me to this answer. The horrible pun strikes back, and yes, I feel bad.