David wrote:
I try to use map container and delete the elements. but there are
something wrong, please help me check it.
class test{
protected:
map<string,myclass*> tests;
public:
test();
void AddMyclass(const string& myname,const myclass &st);
What's the implementation of it? What do you do with 'st'? The
only allowed thing here is a copy-construction in the free store:
...
tests.insert(make_pair(myname, new myclass(st)));
...
~test();
};
suppose myclass has been defined before.
and
test::~test()
{
map<string,myclass*>::iterator ii;
for(ii=tests.begin();ii!=tests.end();++ii)
{
delete(ii->second);
You are only allowed to use 'delete' with a pointer you obtained
by using 'new'. Did you?
}
tests.clear();
No need to call 'clear'. The map is going to be destroyed anyway.
}
int main()
{
test my;
myclass st;
my.AddMyclass("new class",st);
}
But when the code exits from the main function, there was an error
message saying that "Object reference not set to an instance of an
object.".
I think it is caused by the test's destructor function. But I don't
know where is the problem. Please help me. Thanks
You have not shown us the *complete* program. Read FAQ 5.8. The
error is logical, most likely. If you just try to store the _address_
of the local 'st' object, you are not allowed to use 'delete' on it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask