Hi all.... I'm trying to practive & learn about STL and pointers and wrote a little dialog app to help me. It complies but gets an unhandled exception error. Here's my data class..
and instead of the storing MyData objects in a vector in the dialog, I'm trying to store pointers to the MyData objects. So i the MyDialog.h I have this:PHP Code:class MyData
{
public:
MyData();
virtual ~MyData();
void setFirst(int a) { first = a;};
void setName(CString s) { sName = s; };
int getFirst() { return first; };
CString getName() { return sName; };
private:
int first;
CString sName;
}
Then in the dialog to add a MyData object to the vector when I hit Add Mydata Obj I do this:PHP Code:std::vector<MyData*>vPtrs;
int count;// increments number of MyData
and to remove an obj when I press Remove Mydata Obj I do this:PHP Code:void CMyPointerDlg::OnNewobj()
{
//make a new instance of the obj and add it to the vector
MyData *p = new MyData;
p->setFirst(count);
p->setName("NoName");
vPtrs.push_back(p);
int size = vPtrs.size();
m_sObjCount.Format("%d", size);//format for dialog
UpdateData(false);
count++;
}
I get the error on the line marked above... am I totally missing the boat here? Thanks......PHP Code:void CMyPointerDlg::OnRemoveobj()
{
vector<MyData*>::iterator itr;
if(vPtrs.size() > 0)
{
itr = vPtrs.end();
MyData *p;
p= *itr;
int i = p->getFirst();
delete p;//ERROR!
int size = vPtrs.size();
m_sObjCount.Format("%d", size);
UpdateData(false);
}
}




Reply With Quote