vector of classes cloning problem
I need to clone a vector of classes I have it so that it successfully clones but there is two errors: 1) upon exit it causes a crash which i take it is from an object already being deleted & 2) if I add a new object to the newely cloned vector it overwrites the first and last position of the vector.
This is what I have in my program.
I have 2 classes :
1- cnamevaluepair (which holds two string values)
2- cnamevaluepairlist( which has a vector of cnamevaluepair)
the vector def is showed in this typedef
Code:
typedef vector<CNameValuePair*> NVPLIST;
typedef vector<CNameValuePair*>::iterator NVPLISTITER;
I need to have the class clone itself from the main program with a call such as this:
new_list=old_list.clone();
So inside my cnamevaluepairlist.clone() I have this:
Code:
CNameValuePairList CNameValuePairList::clone()
{
CNameValuePairList* temp2=new CNameValuePairList;
string strValue;
string strName;
for (NVPLISTITER the_iterator=list.begin(); the_iterator != list.end(); the_iterator++)
{
strValue=(*the_iterator)->getValue();
strName=(*the_iterator)->getName();
temp2->add(strName,strValue);
}
return *temp2;
}
the list variable is of NVPLIST type. Inside my add(strName,strValue) function i have the following:
Code:
CNameValuePair* temp2=new CNameValue(strName,strValue);
list.push_back(temp2);
Do you know what I am doing wrong to cause the 2 errors stated earlier?