Hi! I'm not sure about using push_back properly in a program I need to get going:
header:Code:vector <string> myvector; // here I'm not sure person::person(string n, int a) { name=n; age=a; set_person(n); } int person::get_age() { return age; } string person::get_name() const { return name; } int person::update_age() { return age+1; } person* person::set_person(string n) { myvector.push_back(n); //it compiles that way //but I think I don't get what I'm supposed to // //from my previous example; without line 13! //people.push_back(n); }
main:Code:#ifndef PERSON_H #define PERSON_H using namespace std; class person { public: vector<person*> people; person(string,int); person* set_person(string); string name; int age; int get_age(); int update_age(); string get_name()const; }; #endif
In the version I used people.push_back(n); I got following failure message:Code:#include <cstdlib> #include <iostream> #include <string.h> /////////////////////////////////////////// #include <iomanip> #include <vector> #include "person.h" using namespace std; int main() { vector<person*> people; bool more=true; while(more) { cout<<"Enter name, q to quit: "; string name; getline(cin,name); int age; if(name=="q") { more=false; } else { people.push_back(new person(name, age)); } } for(int i=0; i<people.size(); i++) { cout<<(*people[i]).get_name(); } while(!people.empty()) { delete people.back(); people.pop_back(); } system("pause"); return 0; }
kind regards,Code:person.cpp:40: error: no matching function for call to 'std::vector<person*, std::allocator<person*> >::push_back(std::string&)' /usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = person*, _Alloc = std::allocator<person*>]
Fellow




Reply With Quote