c++ - Dereferencing iterator to pointer -
i have program using iterator print objects. pointers each object stored in vector. when try print these objects dereferencing iterator, prints memory location. believe because have 1 more layer of indirection due pointers, when dereference again, seg fault haven't been able track down. can find problem?
int main(int argc, const char * argv[]){ objectfactory* objfact = readinput(); (objectfactory::const_iterator p = objfact->begin(); p!=objfact->end(); ++p){ std::cout << *p << " "; } return 0; } objectfactory* readinput(){ std::vector<myobject*> myobjects; objectfactory* objfact = objectfactory::get_inst(); std::string input; int years_passed; std::cin >> years_passed; std::cin >> input; int age = 0; float diameter = 0; while (std::cin){ try{ if (input == "int"){ int value; if (std::cin >> value){ if (value > 0){ age = value; std::cin >> input; } else{ std::cin >> input; throw negativenumber("integer", value); } } else{ std::cin.clear(); std::cin >> input; throw missingvalue("int", input); } } else if (input == "float"){ float value; if (std::cin >> value){ if (value > 0){ diameter = value; std::cin >> input; } else{ std::cin >> input; throw negativenumber("float", value); } } else{ std::cin.clear(); std::cin >> input; throw missingvalue("float", input); } } else if (input == "string"){ std::string value; std::cin >> value; if (diameter == 0 || age == 0){ std::cin >> input; throw incompleteobject(value); } try{ objfact->new_object(value, diameter, age); } catch (invalidage ex){ ex.printmessage(); } std::cin >> input; } else{ std::string tempinput = input; std::cin >> input; throw unrecognizedtype(tempinput); } } catch (unrecognizedtype ex){ ex.printmessage(); } catch (incompleteobject ex){ ex.printmessage(); } catch (negativenumber ex){ ex.printmessage(); } catch (missingvalue ex){ ex.printmessage(); } } return objfact; } class objectfactory { public: static objectfactory* get_inst(); myobject* new_object(std::string name, float diameter, int age); void time_passed(int year); typedef std::vector<myobject*>::const_iterator const_iterator; const_iterator begin() const{return objects.begin();} const_iterator end() const{return objects.end();} protected: objectfactory(){} private: static objectfactory* _instance; std::vector<myobject*> objects; }; objectfactory* objectfactory::_instance=0; objectfactory* objectfactory::get_inst(){ if (_instance ==0) {_instance = new objectfactory;} return _instance; } //create correct object type based of name myobject* objectfactory::new_object(std::string name, float diameter, int age){ if (toupper(name.at(0)) == 'c'){ objects.push_back((myobject*)new car(name, diameter, age)); return objects.at(objects.size()-1); } if (toupper(name.at(0)) == 't'){ objects.push_back((myobject*)new tiger(name, diameter, age)); return objects.at(objects.size()-1); } objects.push_back((myobject*)new human(name, diameter, age)); return objects.at(objects.size()-1); } void objectfactory::time_passed(int year){ (int = 0; < objects.size();i++){ objects[i]->year_passed(year); if (objects[i]->get_age()>objects[i]->get_max_age()){ delete objects[i]; } } } //prints output objecs of myobject type std::ostream& operator << (std::ostream& out, myobject& obj) { out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; } objectfactory* objectfactory::_instance=0; objectfactory* objectfactory::get_inst(){ if (_instance ==0) {_instance = new objectfactory;} return _instance; } //create correct object type based of name myobject* objectfactory::new_object(std::string name, float diameter, int age){ if (toupper(name.at(0)) == 'c'){ objects.push_back((myobject*)new car(name, diameter, age)); return objects.at(objects.size()-1); } if (toupper(name.at(0)) == 't'){ objects.push_back((myobject*)new tiger(name, diameter, age)); return objects.at(objects.size()-1); } objects.push_back((myobject*)new human(name, diameter, age)); return objects.at(objects.size()-1); } void objectfactory::time_passed(int year){ (int = 0; < objects.size();i++){ objects[i]->year_passed(year); if (objects[i]->get_age()>objects[i]->get_max_age()){ delete objects[i]; } } } //prints output objecs of myobject type std::ostream& operator << (std::ostream& out, myobject& obj) { out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; }
i didn't include code myobject or subclasses or error structs i'm using didn't seem relevant, let me know if need it.
the problem caused missing return
statement in
std::ostream& operator << (std::ostream& out, myobject& obj) { out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; }
that lead undefined behavior if use:
std::cout << *(*p) << " ";
add return
statement.
also, change obj
myobject const&
.
std::ostream& operator << (std::ostream& out, myobject const& obj) { out << "(" <<obj.get_type_name() << "," << obj.get_name() << "," << obj.get_diameter() << "," << obj.get_age() << ","<< obj.get_speed() << ")"; return out; }
Comments
Post a Comment