2014-01-09 29 views
0

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);

,但我仍然得到確切的同樣的錯誤。難道我做錯了什麼?

+1

這看你不能只在NULL指針的地方插入一個NULL。只是猜測。 – BWG

+0

該死的,是的,其實指針是問題。我剛剛初始化了一個ClassExample * ptr = NULL;在該行之前並將其轉換爲vectorExample.insert(vectorExample.begin()+ position,ptr);它的工作/ facepalm – Devez

+0

很高興我可以幫助! – BWG

回答

1

嘗試使用

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

在C++ NULL被定義爲0,所以該模板函數不能轉換的int的指針。

+0

我想現在回想起來會比我建議的更好。 – BWG

+0

@BWG我不確定,因爲不是所有的編譯器都支持nullptr。 –

+0

它也僅在C++ 11中受支持,並非所有編譯器都支持。 –

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); 
1

NULL是定義映射爲0。編譯器告訴你,它不能隱式整數轉換爲指針:從整型到指針類型

轉換要求reinterpret_cast,C樣式轉換或函數樣式轉換

所以做什麼編譯器告訴你這樣做:

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

或者:

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