What am I doing wrong? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
0

What am I doing wrong?

https://code.sololearn.com/cIVz1q45no78/?ref=app When I call delete node, it should remove the node from the vector. Why is it not working?

12th Aug 2019, 4:30 PM
Daniel Cooper
5 Respuestas
+ 1
Dennis This is just a learning exercise for me. Just trying to understand pointers and getting a little more problem solving experience. In other words you're going to see a ton of terrible decisions since I'm not really focused on good code right now. Although even when I am focused on that, it's still terrible lol
12th Aug 2019, 5:54 PM
Daniel Cooper
0
The node inside the nodes vector is not the same as the one you passed to it. It's a copy. So their memory addresses will not be the same and the if will never be true. Oh, and deleting from the same container you're iterating without dealing with the iterator correctly is bound to give you more bugs. If your if does evaluate to true you'd end up iterating outside the bounds of your vector in this case.
12th Aug 2019, 5:04 PM
Dennis
Dennis - avatar
0
Dennis How can I fix all of this? I've been struggling to make it work for like a week lol
12th Aug 2019, 5:18 PM
Daniel Cooper
0
A quick fix would be to replace std::vector<node> with std::vector<node*> ( and all the . with -> ), remove the & in if( &*it ... ) and put in a break inside the if. But I'm more wondering why you need to use pointers in the first place. Why not just do something like grid.plot_node( 4, 6 ) and then simply write to data[6][4]? Why not use a 2D array of nodes, without using new, if you really need to store more information than just a character?
12th Aug 2019, 5:38 PM
Dennis
Dennis - avatar
0
Alrighty, that makes sense. :)
12th Aug 2019, 5:55 PM
Dennis
Dennis - avatar