矢量初始化大小,无法清除

发布于 2024-09-30 06:41:30 字数 1578 浏览 6 评论 0原文

我有一个多维向量的声明

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

然后我在创建它时将其发送到我的类:

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

然后我执行以下操作:

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);
    }
}

我想要做的是用 向量填充主 front_objects ofxImages 来自 Catalog_objects,它可能共享 ofxImages 向量的一些向量。

问题是,“有时”向量是用垃圾初始化的,当试图用

front_objects[numTag].resize(2);

程序清除它时,程序会因 EXC_BAD_ACCESS 崩溃。

当使用 resize() 调整它的大小时,它不应该用空向量填充吗?

谢谢马克

更新

我尝试这样做,但我得到“未初始化的引用成员'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 &

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 &

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-10-07 06:41:30

numTag中很可能

front_objects[numTag].resize(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.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文