c++ - How to add a Node pointer to a Vector pointer? -
c++ - How to add a Node pointer to a Vector pointer? -
i trying create maze consists of nodes objects. each node object has fellow member variable node *attachednodes[4]
contains of attached nodes later tell programme options has when doing breadth first search. every time think understand pointers, issue comes up, , sense lost on again. since working fine (as far knew) until changed thought unrelated. anyways, here issues are:
my node object looks this
class node { public: ... void attachnewnode(node *newnode, int index); ... private: ... node *attachednodes[4]; ... };
my function attach nodes looks this:
void node::attachnewnode(node *newnode, int index) { *attachednodes[index] = *newnode; }
and lastly, part of other function calling attachnewnode function looks this:
int mazeindex = 0; while (instream.peek() != eof) { int count = 0; node n; node m; ... if (system::isnode(name2)) { m = system::findnode(name2); } else { m = node(name2); maze[mazeindex] = m; mazeindex++; } node *temp; *temp = m; n.attachnewnode(temp, count); //the error happens here, added rest of code because through debugging consistently in whole area. count++; } n.setnumberused(count); }
sorry got little lengthy, i've been searching on portion have provided trying figure out wrong, nice have knows little more pointers give input on matter. node class given me, else made, of changed. in advance help.
your class contains property:
node *attachednodes[4];
the above says attachednodes array contains 4 pointers nodes. in attachnewnode function, do:
*attachednodes[index] = *newnode;
this means trying assign value of newnode (as * dereferences pointer) value of element under attachednodes[index]. want is:
attachednodes[index] = newnode;
this means want store address (as pointer address place in memory) in array of addresses.
there error here:
node *temp; *temp = m; n.attachnewnode(temp, count);
again, interested in storing address of node m. in order that, need said address:
node *temp; temp = &m; n.attachnewnode(temp, count);
these obvious problems above code, there might more.
c++ pointers vector nodes
Comments
Post a Comment