Hello,
I am just learning c++ and I'm trying to create heap-based objects. In the destructor of the class, I must delete the object from the heap. My question is: How can I delete the heap objects that I put into a vector?
For example: this is a Sample class. This class has a vector called myVector and Testing is another object.
I get this error while compiling:Code://File: Sample.cc #include <string> #include <vector> #include "Testing.h" using namespace std; Sample::Sample() {} Sample::~Sample() { for (int i=0; i<myVector.size(); i++) { delete myVector[i]; } } void Sample::sampleMethod(string myValue) { Testing *t; //create an new Testing object, t, on the heap t = new Testing(); t->setValue(myValue); //set the value of t myVector.push_back(*t); //put t into a vector }
Sample.cc: In method `Sample::~Sample()':
Sample.cc:11: type `class Testing' argument given to `delete', expected pointer
Does anyone know what is wrong?
How can I delete the objects in the vector?
Thanks alot!



LinkBack URL
About LinkBacks



)