Question

I tried to declare a struct,

struct Sample {
    int classLabel[2];
    vector<Mat> image; 
};

Now i want to use it, i initialize it as

Sample sample1;

and save 5 images in sample1, and created another Sample sample2 and saved another 5 images. Now i want to save these two samples in a vector of Sample. I declared:

vector<Sample> samples;

now when i try to push_back sample1 and sample2 in samples

samples.push_back(sample1);
samples.push_back(sample2);

it gives me nothing. the samples shows as samples[2] but includes nothing with question marks infront of classLabel=??? and image=????

Can any body guide me where i am doing mistake. How can i make it useable. i mean properly save that struct sample1 and sample2 in samples. Will be thankful. Regards

No correct solution

OTHER TIPS

vector<Sample> samples;
samples.push_back(sample1);
samples.push_back(sample2);

After this, the size of samples is 2. So you can only access samples[0] and samples[1], not samples[2].


As commented by @sammy, variables in Watch may show incorrect values in VS.

To make it work, you may want to follow the following steps:

  1. Check if the application and its dependencies (if required) are built in debug mode.
  2. Ensure the Optimization is disable. Use the /Od option when compiling.
  3. Struct Member Alignment. When there are a bunch of projects compiled together, with varying values for this field inside the projects could cause invalid display values. Reset to Default value on all of the projects.The value is under Configuration Properties-> C/C++ ->Code Generation -> Struct Member Alignment.

Check out here for more info.


Edit: For your code in the comment:

sample1.classlabel[0] = 0; 
sample1.classlabel[1]=1;
vector<Mat> temp; 
temp.push_back(img1); 
temp.push_back(img2); 
temp.push_back(img3); 
sample1.image=temp; 

sample2.classlabel[0] = 0; 
sample2.classlabel[1]=1; 
vector<Mat> temp1; 
temp.push_back(img5);         // problem
temp.push_back(img6);         // problem
temp.push_back(img7);         // problem
sample2.image=temp1; 

samples.push_back(sample1); 
samples.push_back(sample2);

The 3 problem lines should change to

temp1.push_back(img5);
temp1.push_back(img6);
temp1.push_back(img7); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top