2014-01-09 3 views
0

C++ 초보자는 여기에 있습니다. 나는 Bloodshed Dev C++을 사용하여 프로젝트를 끝냈습니다. 이제는 Visual C++ 프로젝트로 만들고 싶습니다. OpenGL을 사용하는 방법을 배우고 대부분의 자습서는 데모 용으로 나중에 사용하기 때문에 프로젝트를 완료했습니다. 그것은 익숙하지만 Visual C++에서 벡터 삽입()을 컴파일 할 수 없습니다.

, 나는 전혀 그렇게 내 질문에 가혹하지 iterator들에 대한 지식없이 insert()erase()를 사용하여 저를 이끌어 객체에 대한 포인터의 동적 배열을 관리 할 vector을 사용했다 . 문제는 삽입 된 위치를 지정하기 위해 계산 된 int을 사용하여 벡터에 새 항목을 삽입하는 선입니다 (비록 컴파일러 오류를 일으키는 것이 아니라고 확신하지만). 내가이 줄을 컴파일 할 때

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

모든 컴파일 및 ++ 데브 C뿐만 비주얼 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

그것은 참조 ms 당신은 NULL 포인터 대신 널 포인터를 쓸 수 없습니다. 그냥 추측 해. – BWG

+0

젠장, 실제로 포인터가 문제입니다. 방금 ClassExample을 초기화했습니다. * ptr = NULL; 선 앞에 놓고 vectorExample.insert (vectorExample.begin() + position, ptr); 그리고 그것은 일했다/facepalm – Devez

+0

도움이 될 수있어 기쁘다! – BWG

답변

1

봅니다 0 그래서 템플릿 함수가 포인터하는 int 변환 할 수있는 C++ NULL에서

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

가 정의되어 사용할 수 있습니다.

+0

회상에서 내가 제안한 것보다 나을 것 같아요. – BWG

+0

@BWG 모든 컴파일러가 nullptr을 아직 지원하지 않기 때문에 확실하지 않습니다. –

+0

또한 모든 컴파일러가 아직 지원하지 않는 C++ 11에서만 지원됩니다. –

1

나는 그것을 줄 것이다.

NULL의 정의를 보면, 단지 0 또는 0L입니다. 그러나 귀하의 벡터는 ClassExample * 유형을 허용합니다. 포인터는 기본적으로 int이지만 int에 넣을 수는 없습니다. 그리고 NULL은 int입니다. A는 그 공에 매핑 정의되어

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

NULL :

는이 문제를 해결하려면, 당신이 뭔가를 할 수 있으리라 생각합니다.컴파일러는 암시 적으로 포인터에 정수를 변환 할 수 있음을 말하고있다 : 포인터 타입 필수 유형에서

변환이 reinterpret_cast를 요구, C 스타일 캐스트 또는 함수 스타일 캐스트

그래서 무엇을 컴파일러는 당신이해야 할 일 알려줍니다 :

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

또는 :

vectorExample.insert(vectorExample.begin() + position, (ClassExample*)NULL); 
관련 문제