Visual C ++无法编译vector insert()

时间:2014-01-09 02:36:10

标签: c++ vector iterator

C ++新手在这里。我已经使用 Bloodshed Dev C ++ 完成了一个项目,现在我想将它变成一个 Visual C ++ 项目,因为我想学习如何使用 OpenGL < / i>在其中,大多数教程使用后者进行演示。

虽然不熟悉它,但我使用了vector来管理指向对象的动态指针数组,这使我使用insert()erase()而不了解{{ 1}} ss,所以不要对我的问题苛刻。问题是我使用计算的iterator在向量中插入一个新项来指定要插入的位置的行(虽然我很确定这不是导致编译器错误的原因 - &gt;参见结尾的帖子) 。这一行是(从这里开始,我用实例替换了实际名称):

int

Dev C ++ 中编译和工作没有任何问题,但在 Visual C ++ 中,当我尝试编译此行时,我得到以下错误(它编译时没有它和程序适用于其他一切):

vectorExample.insert(vectorExample.begin() + position, NULL);

我一直在寻找例子并且连续两天搜索,我找不到任何与我的问题类似的东西。我也尝试过:

1>c:\program files\microsoft visual studio 10.0\vc\include\xmemory(208): error C2440: 'initializing' : cannot convert from 'int' to 'ClassExample *' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 1> c:\program files\microsoft visual studio 10.0\vc\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<int>(ClassExample **,_Other &&)' being compiled 1> with 1> [ 1> _Ty=ClassExample *, 1> _Other=int 1> ] 1> c:\program files\microsoft visual studio 10.0\vc\include\vector(668) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,ClassExample*,int>(_Alloc &,_Ty1 *,_Ty2 &&)' being compiled 1> with 1> [ 1> _Ty=ClassExample *, 1> _Alloc=std::allocator<ClassExample *>, 1> _Ty1=ClassExample *, 1> _Ty2=int 1> ] 1> c:\program files\microsoft visual studio 10.0\vc\include\vector(688) : see reference to function template instantiation 'void std::vector<_Ty>::emplace_back<int>(_Valty &&)' being compiled 1> with 1> [ 1> _Ty=ClassExample *, 1> _Valty=int 1> ] 1> c:\program files\microsoft visual studio 10.0\vc\include\vector(675) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::emplace<int>(std::_Vector_const_iterator<_Myvec>,_Valty &&)' being compiled 1> with 1> [ 1> _Myvec=std::_Vector_val<ClassExample *,std::allocator<ClassExample *>>, 1> _Ty=ClassExample *, 1> _Valty=int 1> ] 1> c:\users\user\desktop\mycppproject\mycppfile.cpp(412) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert<int>(std::_Vector_const_iterator<_Myvec>,_Valty &&)' being compiled 1> with 1> [ 1> _Myvec=std::_Vector_val<ClassExample *,std::allocator<ClassExample *>>, 1> _Ty=ClassExample *, 1> _Valty=int 1> ]

但我仍然得到完全相同的错误。我做错了吗?

3 个答案:

答案 0 :(得分:1)

尝试使用

vectorExample.insert(vectorExample.begin() + position, nullptr);

在C ++中,NULL被定义为0.因此模板函数不能将int转换为指针。

答案 1 :(得分:1)

我会试一试。

如果查看NULL的定义,则只有00L。但是,您的向量接受类型ClassExample *。虽然指针本质上是一个int,但你不能只放入一个int。而NULL只是一个int。

要解决这个问题,我相信你可以这样做:

ClassExample* p = NULL; //assigning a pointer to NULL (0) is alright
vectorExample.insert(vectorExample.begin() + position, p);

答案 2 :(得分:1)

NULL是一个映射到0的定义。编译器告诉你它不能隐式地将整数转换为指针:

  

从积分类型到指针类型的转换需要reinterpret_cast,C风格的转换或函数式转换

编译器告诉你这样做:

vectorExample.insert(vectorExample.begin() + position, reinterpret_cast<ClassExample*>(NULL));

或者:

vectorExample.insert(vectorExample.begin() + position, (ClassExample*)NULL);