可视C++无法编译矢量插入()
Visual C++ cannot compile vector insert()
C++新手在这里。我已经完成了使用Bloodshed Dev C++做一个项目,现在我想把它做成一个Visual C++项目,因为我想学习如何在其中使用OpenGL,大多数教程都使用后者进行演示。
虽然不熟悉它,但我使用vector
来管理指向对象的指针的动态数组,这导致我在完全不了解iterator
的情况下使用insert()
和erase()
,所以不要对我的问题苛刻。问题是我使用计算int
在向量中插入新项目的行,以指定要插入的位置(尽管我相当确定这不是导致编译器错误的原因 ->请参阅帖子末尾)。该行是(从这里开始,我用示例替换了实际名称):
vectorExample.insert(vectorExample.begin() + position, NULL);
在DevC++ 中,一切都可以编译和工作,没有任何问题,但在Visual C++中,当我尝试编译此行时,出现以下错误(它在没有它的情况下编译,程序适用于其他所有内容):
1>c:program filesmicrosoft visual studio 10.0vcincludexmemory(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 filesmicrosoft visual studio 10.0vcincludexmemory(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 filesmicrosoft visual studio 10.0vcincludevector(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 filesmicrosoft visual studio 10.0vcincludevector(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 filesmicrosoft visual studio 10.0vcincludevector(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:usersuserdesktopmycppprojectmycppfile.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> ]
我一直在查看示例并连续两天搜索,但我找不到与我的问题类似的东西。我也试过:
vectorExample.insert(vectorExample.begin(), NULL);
但我仍然收到完全相同的错误。我做错了什么吗?
尝试使用
vectorExample.insert(vectorExample.begin() + position, nullptr);
在C++ NULL 定义为 0。因此,模板函数无法将 int 转换为指针。
我会试一试。
如果你看一下NULL
的定义,它只是0
或0L
。但是,您的向量接受类型ClassExample *
。虽然指针本质上是一个 int,但你不能只输入一个 int。NULL
就是这样,一个整数。
为了解决这个问题,我相信你可以做这样的事情:
ClassExample* p = NULL; //assigning a pointer to NULL (0) is alright
vectorExample.insert(vectorExample.begin() + position, p);
NULL
是一个映射到 0 的定义。 编译器告诉您它不能将整数隐式转换为指针:
从整型转换为指针型需要 reinterpret_cast、C 样式转换或函数样式转换
因此,请执行编译器告诉您的操作:
vectorExample.insert(vectorExample.begin() + position, reinterpret_cast<ClassExample*>(NULL));
或:
vectorExample.insert(vectorExample.begin() + position, (ClassExample*)NULL);