Originally posted by lostminds
So are you saying instead of creating a local variable in CNamePairList::add() for my iterator I should put it in the private section of the class? Which would stop that error of overwriting to occur?
Yes the vector of pointers ... but the requirements for this program says I must use those typedefs. *sigh*
If you are stuck with that typedef, then you must work around it....but creating that object on the stack (as you are when you CNamePair temp2(strName,strValue);) and then storing a pointer to that object for later use is a surefire way to get into trouble......
You could create the objects on the heap...but then you must explicitly destroy them when your class dies...like so
Code:
#include <iostream>
#include <string>
#include <vector>
#include <utility>//for std::pair
using namespace std;
class foobar{
typedef vector<pair<string, string>* > MYVECTOR;
typedef MYVECTOR::iterator MYVECTORITER;
MYVECTOR vec;
public:
void AddPair(const string& lhs,const string& rhs){
MYVECTORITER the_iterator;
pair<string,string>* temp = new pair<string,string>(lhs,rhs);
vec.push_back(temp);
for (the_iterator = vec.begin();
the_iterator != vec.end();
++the_iterator){
cout << (*the_iterator)->first << ' ';
cout << (*the_iterator)->second << '\n';
}
}
~foobar(){//destructor
for(int i = 0;i < vec.size();++i)
delete vec[i];//delete each
}
};
int main(){
foobar f;
f.AddPair("Hello","World");
cout << endl;
f.AddPair("Eat at","Joes");
cout << endl;
f.AddPair("foo","bar");
}