小编Max*_*Max的帖子

不匹配operator =使用std :: vector

我有一个这样的类声明:

class Level
{
    private:
        std::vector<mapObject::MapObject> features;
    (...)
};
Run Code Online (Sandbox Code Playgroud)

在其中一个成员函数中,我尝试迭代遍历该向量,如下所示:

vector<mapObject::MapObject::iterator it;
for(it=features.begin(); it<features.end(); it++)
{
    /* loop code */
}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很简单,但是g ++给了我这个错误:

src/Level.cpp:402: error: no match for ‘operator=’ in ‘it = ((const yarl::level::Level*)this)->yarl::level::Level::features.std::vector<_Tp, _Alloc>::begin [with _Tp = yarl::mapObject::MapObject, _Alloc = std::allocator<yarl::mapObject::MapObject>]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669:注意:候选人是:__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std :: vector >>&__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std :: vector>>::operator=(const __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*, ``std::vector<yarl::mapObject::MapObject, std::allocator<yarl::mapObject::MapObject> > >&)

任何人都知道为什么会这样吗?

c++ stl const vector

2
推荐指数
1
解决办法
1万
查看次数

g ++中的奇怪错误

我的程序有一个类,其中包含一个Level名为的对象向量levels.在成员函数中有这一行:

levels.push_back(level::Level());
Run Code Online (Sandbox Code Playgroud)

我今天对我的程序做了一些更改,这行代码开始了segfaulting:

0x0804d248 in void std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::emplace_back<yarl::level::Level>(yarl::level::Level&&) (this=0x0, 
__args#0=...) at /usr/include/c++/4.4/bits/vector.tcc:93
93      if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
Run Code Online (Sandbox Code Playgroud)

我认为Level对象可能以某种方式变得腐败,所以我在函数调用之外声明它,所以我可以在gdb中检查它,如下所示:

level::Level foo();
levels.push_back(foo);
Run Code Online (Sandbox Code Playgroud)

原来这不编译.它g ++给出了我以前没见过的两个错误:

error: invalid conversion from 'level::Level (*)()' to 'int'
error: initializing argument 1 of 'level::Level::Level(int, int, int)'
Run Code Online (Sandbox Code Playgroud)

现在,Level构造函数接受三个整数参数,每个参数都有默认值.我以为它可能一直在抱怨我没有传递这三个参数,即使它们有默认值,所以我更改了第一行以传递默认值:

level::Level foo(1, 100, 100);
Run Code Online (Sandbox Code Playgroud)

这现在编译,但仍然是段错误,但在不同的地方(尽管是相同的测试)这样做:

0x0804c699 in std::vector<yarl::level::Level, std::allocator<yarl::level::Level> >::push_back (this=0x0, __x=...) at /usr/include/c++/4.4/bits/stl_vector.h:735
735     if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
Run Code Online (Sandbox Code Playgroud)

我意识到这个代码太少,不能指望你们能够解决我的问题,但也许有人可以告诉我更多关于这些错误的含义?特别是那些g ++错误; 我不知道为什么它不会接受一个空的Level构造函数,因为它的所有参数都是默认的,我不知道(*)()错误的部分是什么意思(它使得错误对谷歌来说非常令人沮丧).

c++ segmentation-fault

1
推荐指数
1
解决办法
2319
查看次数

标签 统计

c++ ×2

const ×1

segmentation-fault ×1

stl ×1

vector ×1