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);
하지만, 난 여전히 똑같은 오류를 얻을. 내가 뭔가 잘못하고 있는거야?
그것은 참조 ms 당신은 NULL 포인터 대신 널 포인터를 쓸 수 없습니다. 그냥 추측 해. – BWG
젠장, 실제로 포인터가 문제입니다. 방금 ClassExample을 초기화했습니다. * ptr = NULL; 선 앞에 놓고 vectorExample.insert (vectorExample.begin() + position, ptr); 그리고 그것은 일했다/facepalm – Devez
도움이 될 수있어 기쁘다! – BWG