c++ - Iterator prints two values, one is text and other is unfortunately a pointer -
the following code shows 4 messages, first 2 normal, other 2 passes through iterator, second 1 doesn't show right value, prints pointer instead of value, code same 2 variables.
the code big, important part inside main , show_message, problem anm->getname() works , anm->getparent()->getname() doesn't
#define win32_lean_and_mean #include <windows.h> #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> #include <string> #include <vector> class node; class customobject; class customobject { public: std::string name; node* parent; customobject(std::string n_name); std::string getname(); void setparent(node* pparent); node* getparent(); }; customobject::customobject(std::string n_name) { name = n_name; } std::string customobject::getname() { return name; } void customobject::setparent(node* pparent) { parent = pparent; } node* customobject::getparent() { return parent; } class node { public: std::string name; node* parent; std::vector<node> childs; node(std::string n_name); void attach(node* nd); void attach(customobject* anm); std::string getname(); void setparent(node* pparent); node* getparent(); std::vector<customobject> mcustomobjects; }; node::node(std::string n_name) { name = n_name; } std::string node::getname() { return name; } void node::setparent(node* pparent) { parent = pparent; } node* node::getparent() { return parent; } void node::attach(node* nd) { childs.push_back(*nd); nd->setparent(this); } void node::attach(customobject* anm) { mcustomobjects.push_back(*anm); anm->setparent(this); } class game { std::string name; public: std::vector<node> nodes; std::vector<customobject> mcustomobjects; game(std::string nm); }; game::game(std::string nm) { name = nm; } void show_message(customobject* anm) { messagebox( null, anm->getname().c_str(),"message", 0); messagebox( null, anm->getparent()->getname().c_str(),"message", 0); } int main() { game* mgame = new game("mygame"); node* mnode = new node("mynode"); mgame->nodes.push_back(*mnode); customobject* mcustomobject = new customobject("object1"); mgame->mcustomobjects.push_back(*mcustomobject); mnode->attach(mcustomobject); show_message(mcustomobject); (std::vector<customobject>::iterator itr = mgame->mcustomobjects.begin(); itr != mgame->mcustomobjects.end(); ++itr) { customobject* cobj; cobj = &(*itr); show_message(cobj); } }
so, idea what's wrong code?
thank you.
here 1 problem, , symptom of program:
void node::attach(customobject* anm) { mcustomobjects.push_back(*anm); anm->setparent(this); }
you're calling function, take @ closely. first line dereferences pointer , places copy of customobject
in vector. next line sets parent, not of copy placed in vector, passed-in pointer.
so in end, vector winds copies have bogus parent pointers. lucky got see output @ all.
crashing example windows stuff removed
in addition this, have memory leaks in various places. code doesn't call delete
@ all.
the overall problem seems overusing pointers point you're not sure whether use value types or pointers. suggest scrap of pointer-isms (or of them), , use values. if determined yes, must use pointer should use them. if use pointers, attempt use smart pointer (std::unique_ptr
, std::shared_ptr
, etc.)
Comments
Post a Comment