On 2007-12-21 23:58:12 -0500, bahoo <b8*******@yahoo.comsaid:
Hi,
In the main function, I have
vector<vector<Store>stores;
vector<Storev(11);
stores.push_back(v);
and here is the Store class:
class Store
{
public:
Store();
IplImage* img;
};
Store::Store()
{
img = cvCreateImage(cvSize(512,512), IPL_DEPTH_32F, 1);
}
The strange thing is that all the 11 "v" have the same "img", with the
same address in the memory. What I wanted is of course 11 "v" with
different "img".
Can anyone spot the error?
Thanks
bahoo
I just found this out myself. It turns out that the way std::vector
initializes its array is this:
1) create a default object,
2) use the copy constructor to create all the elements of the array.
In your case, it created one copy of the default Store object. Then
the copy constructor simply made 11 copies each copying from the same
img pointer value.
So maybe you should do this instead:
vector<Storev;
for(int i = 0; i < 11; i++) v.push_back(Store());
--
-kira