Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

follow your nodes

Name: Anonymous 2010-04-28 14:19

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: Anonymous 2010-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: Anonymous 2010-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.

Thanks for the input.

Name: Anonymous 2010-04-28 16:04

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: Anonymous 2010-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.

Name: Anonymous 2010-04-28 19:39

node my anus

Name: Anonymous 2010-04-28 22:07

Name: Anonymous 2010-04-29 1:22

follow your nodes
That is a bad pun and you should feel bad.

Name: Anonymous 2010-04-29 14:55

>>8
Sorry for the terrible pun, but it was relevant.

All right, now I'm having problems dealing with a double free or memory corruption error. 

The node->data is now an object that contains a pointer to ints used to create an array.

class intobject{
     private:
          int* array;

     public:
          Image();
          Image(intobject &new_int);
          ~Image();
          int* get_array() const { return array; }
          void set_array(int* new_int);
};


List code

struct node {
     intobject data;
     struct node* next;
}; 

class List
{       
     public:
    
     List();
     List(List &new_list);
     ~List();
     struct node* create_node(intobject new_int);
     void insert_node(struct node* new_node, struct node* prev_node);
     int get_num_nodes() const { return num_nodes; }
     struct node* get_iList() const { return iList; }
     void set_num_nodes(int new_numNodes);
     void set_iList(struct node* new_iList);
        
     private: 
       
     struct node* iList;
     int num_nodes;
};


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.

Name: Anonymous 2010-04-29 14:56

Crap, meant

class intobject{
     private:
          int* array;

     public:
          intobject();
          intobject(intobject &new_int);
          ~intobject();
          int* get_array() const { return array; }
          void set_array(int* new_int);
};

Name: Anonymous 2010-04-29 16:42

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.

Don't change these.
Name: Email:
Entire Thread Thread List