C++新手在這裏。我已經完成了一個項目,使用 Bloodshed Dev C++,現在我想將它製作成 Visual C++項目,因爲我想學習如何在其中使用 OpenGL,大多數教程都使用後者進行演示。Visual C++無法編譯向量插入()
雖然不熟悉它,我使用了一個vector
管理對象指針的動態數組,害得我用insert()
和erase()
不具有iterator
小號任何知識都這麼不苦待我的問題。問題是我在向量中插入一個新項目的行,使用計算出的int
來指定要插入的位置(儘管我相當確定這不是導致編譯器錯誤的原因 - >請參閱末尾)。該生產線是(從這裏開始我已經取代實際的名稱舉例):
vectorExample.insert(vectorExample.begin() + position, NULL);
一切編譯和開發的C++但的Visual C++沒有任何問題的作品,當我嘗試編譯此行我收到以下錯誤(它編譯沒有它和程序適用於一切):
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> ]
我一直在尋找的例子和直搜索了兩天,我找不到類似我的問題什麼。我也試過:
vectorExample.insert(vectorExample.begin(), NULL);
,但我仍然得到確切的同樣的錯誤。難道我做錯了什麼?
這看你不能只在NULL指針的地方插入一個NULL。只是猜測。 – BWG
該死的,是的,其實指針是問題。我剛剛初始化了一個ClassExample * ptr = NULL;在該行之前並將其轉換爲vectorExample.insert(vectorExample.begin()+ position,ptr);它的工作/ facepalm – Devez
很高興我可以幫助! – BWG