0

I have this declaration of a multidimensional vector

std::vector< vector < vector  < ofxImage > > > front_objects;

Then I send it to my class when creating it:

Catalog_object * temp = new Catalog_object(&front_objects, numTag);

And then I perform the following:

Catalog_object::Catalog_object(vector< vector < vector  < ofxImage > > > * _front_objects, int numTag) {
    front_objects=_front_objects;
    if (front_objects->size()<numTag+1) {
        front_objects->resize(numTag+1);
    }
}

What I want to do is to populate the main front_objects with vectors of ofxImages from the Catalog_objects, which might share some vectors of vectors of ofxImages.

The problem is that "sometimes" the vector is initialized with garbage and when trying to clear it with

front_objects[numTag].resize(2);

the program crashes with an EXC_BAD_ACCESS

When resizing it with resize(), shouldnt it be filled with empty vectors?

Thanks

Marc

UPDATE

I tried doing like this but I get "uninitialized reference member 'Catalog_object::front_objects'".

Catalog_object::Catalog_object(vector< vector < vector  < ofxImage > > > & _front_objects, int numTag) { // CHANGED * FOR &
    std::vector< vector  < vector < ofxImage > > > & front_objects; // CHANGED * FOR &
    front_objects=_front_objects;
    if (front_objects.size()<numTag+1) {
        front_objects.resize(numTag+1);
    }
    front_objects[numTag].resize(2);
}


std::vector< vector < vector  < ofxImage > > > front_objects;
Catalog_object * temp = new Catalog_object(front_objects, numTag); // REMOVED &
3
  • Tangential comment: This is really a canonical example of when typedefs would be a good thing! Nov 4, 2010 at 12:11
  • I am a c++ newbie, I'll check typedefs
    – Marc
    Nov 4, 2010 at 12:22
  • I just learnt how to use typedefs. Very easy and useful! functionx.com/cpp/keywords/typedef.htm
    – Marc
    Nov 8, 2010 at 9:38

1 Answer 1

2

Most likely in

front_objects[numTag].resize(2);

numTag holds an invalid index.

Without the relevant code is difficult to say. Effectively you're saying that every time you walk into your kitchen you hear a strange sound. The picture of the cat that's stuck in between two somethings makes it likely that it's the cat, but could be something else. :-)

Cheers & hth.,

5
  • Thanks Alf, but I already took care of that when I did front_objects->resize(numTag+1); , or am I wrong? I'm worried that I could be messing with the pointers ("pointing to the wrong places")
    – Marc
    Nov 4, 2010 at 12:26
  • 1
    @Mark: oh **** (censored). You mean that that resize call is within your constructor, where front_objects has been declared as a pointer? Uh oh. If so then you're using that pointer as an array, and then here's how to fix: pass it by reference instead of as a pointer. That is, replace your formal param * with a &, and replace -> with ., and drop the address operator in the invocation. Cheers & hth., Nov 4, 2010 at 12:39
  • Thanks Alf. Yes, the resize is inside of the Catalog_object constructor, maybe in the update that I have made to my code is clearer. I tried to apply your suggestions, but I get an "uninitialized reference member" error. I'm not good at pointers yet, so probably I have done something else wrong now! :S My intention is that all the modifications are made always to the same front_objects
    – Marc
    Nov 4, 2010 at 13:01
  • @Marc: if I understand this correctly, in the original code front_objects was a member variable, of type pointer to vector of vector of vector of something. Presumably you still have that member variable. To make things simple, rename the member, e.g. p_front_objects. Then initialize it as address of the formal argument. You can still use a local reference declaration for convenience. If so then the local reference needs to be initialized. Cheers & hth., Nov 4, 2010 at 13:20
  • thanks for your help, but I saw that I had several problems that made me restructure the code. I finally made the vector a static member of the Catalog_object class, which made things easier and I didn't encounter the problem above. However that's something that I'll have to chech again at some point! Thank you for your patience, Marc
    – Marc
    Nov 8, 2010 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.