473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about vectors

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

Dec 22 '07 #1
2 1120
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

Dec 22 '07 #2
On Dec 21, 11:58*pm, bahoo <b83503...@yahoo.comwrote:
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?
The error is that you don't have a correct copy constructor and
assignment op in your class. The vector uses the defaults wich don't
do what you want. I expect that your Store class had a destructor
(though you didn't post the code here.) Anytime a class needs a custom
destructor, it is very likely it needs a custom assignment operator
and copy constructor as well.

The proper solution is to write a copy constructor for your class.
Dec 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: da Vinci | last post by:
OK, this is a pretty weird question, but shouldn't be to hard to accomplish. Remember, I am a beginner so please, if I say something stupid, be gentle. :-) I do not know vectors yet, which would...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
9
by: Jeff | last post by:
Hello- Ive never used a vector or vectors in C++ so I have a question for you all. I know I can dynamically create the size I need upfront, but is it possible to create them on the fly...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
10
by: perochak | last post by:
Write a C++ program to calculate the GPA of students. Your program should contain a class named Gpa. The class should have an array named result, as its data member. This array is meant to take...
6
by: %NAME% | last post by:
The title is not exactly a disnomer, so pleaes let me explain... Say I have a large number of 0-1 vectors containing only bits corresponding to 0 or 1. Now given an arbitrary 0-1 vector, I wish...
7
by: Markus Pitha | last post by:
Hello again, I have another question about the handling of multidimensional vectors. Actually, it's not difficult when the size is known at the beginning, but I still wasn't able to create...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.