矢量初始化大小,无法清除
我有一个多维向量的声明
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
numTag
中很可能包含无效索引。
没有相关代码就很难说了。实际上,您是在说每次您走进厨房时都会听到奇怪的声音。猫被夹在两个东西之间的图片使得它很可能是猫,但也可能是其他东西。 :-)
干杯&呵呵,
Most likely in
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.,