i have a table view where each cell (in the model) is associated with a "Counter" object. all the Counter objects are saved in std::vector<Counter> pCounters .
problem is whenever i try to delete any object from that vector, no matter what i do the std::vector erase() function always deletes the last one.
i made two versions of the delete function just to demonstrate what things i tried which always lead to the same result. if any of this versions work (as anticipated) then problem solved.
i will try to keep this post Simple, however if any information is needed i can add it later on.
//Counter.h
//copy constructor of the Counter class
Counter(const Counter& c)
:
ClickTimes(c.ClickTimes), //unsigned int
name(c.name), //Qstring
firstTime(c.firstTime), //bool
CreationDate(c.CreationDate), //Const QDate
lastDate(c.lastDate), //QDate
pumped(c.pumped) //bool
{}
// operator =() overload of Counter Class (i think the problem is here)
Counter operator=(const Counter& rhs)
{
Counter lhs(rhs);
return lhs;
}
//MainWindow.cpp
//delete Function version 1
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);
QString itsName
= model
->item
(row,
0)->text
();
//name of the row for(auto i = pCounters.begin();i != pCounters.end();i++)
{
if(i->getName() == itsName)
{
pCounters.erase(i);
saved = false;
return; //program crashes if its the last entry otherwise it deletes the last entry
}
}
}
//Delete Function Version 2
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);
if(pCounters.size() > row)
{
pCounters.erase(pCounters.begin()+ row);
saved = false;
//doesn't crash but always deletes the last entry no matter where the iterator is
//all these trials(down) yield the same result
//pCounters.erase(pCounters.end()- row);
//pCounters.erase(pCounters.begin());
//pCounters.erase(pCounters.end());
}
}
//Counter.h
//copy constructor of the Counter class
Counter(const Counter& c)
:
ClickTimes(c.ClickTimes), //unsigned int
name(c.name), //Qstring
firstTime(c.firstTime), //bool
CreationDate(c.CreationDate), //Const QDate
lastDate(c.lastDate), //QDate
pumped(c.pumped) //bool
{}
// operator =() overload of Counter Class (i think the problem is here)
Counter operator=(const Counter& rhs)
{
Counter lhs(rhs);
return lhs;
}
//MainWindow.cpp
//delete Function version 1
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);
QString itsName = model->item(row,0)->text(); //name of the row
for(auto i = pCounters.begin();i != pCounters.end();i++)
{
if(i->getName() == itsName)
{
pCounters.erase(i);
saved = false;
return; //program crashes if its the last entry otherwise it deletes the last entry
}
}
}
//Delete Function Version 2
void MainWindow::DeleteItem(int row,int col = 0) //delete by right click
{
model->removeRow(row);
if(pCounters.size() > row)
{
pCounters.erase(pCounters.begin()+ row);
saved = false;
//doesn't crash but always deletes the last entry no matter where the iterator is
//all these trials(down) yield the same result
//pCounters.erase(pCounters.end()- row);
//pCounters.erase(pCounters.begin());
//pCounters.erase(pCounters.end());
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks