2012-06-22 2 views
2

Linux (Redhat)에서 이전 C++ 코드를 실행하려고합니다. gcc 버전 4.1.2를 사용하고 있습니다.오류 : long int에서 비 스칼라 유형으로 변환 __gnu_cxx :: __ normal_iterator

  /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h: In member   function âTP* GCVVector<TP>::Find(const TP&) [with TP = GCVAsso<GCVString, GCVString>::KeyNode]â: 
     /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVAsso.h:165: instantiated from âbool GCVAsso<KTP, VTP>::Add(KTP, VTP) [with KTP = GCVString, VTP = GCVString]â 
     /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.h:69: instantiated from here 
     /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h:398: error: conversion from âlong intâ to non-scalar type â__gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >â requested 
      /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVAsso.h:165: instantiated from âbool GCVAsso<KTP, VTP>::Add(KTP, VTP) [with KTP = GCVString, VTP = GCVString]â 
     /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.h:69: instantiated from here 
     /trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVVector.h:403: error: no match for âoperator=â in âpCurrent = GCVVector<TP>::BinarySearch [with TP = GCVAsso<GCVString, GCVString>::KeyNode](0l, (GCVVector<TP>::GetSize [with TP = GCVAsso<GCVString, GCVString>::KeyNode]() - 1l), ((const GCVAsso<GCVString, GCVString>::KeyNode&)((const GCVAsso<GCVString, GCVString>::KeyNode*)Obj)))â 
     /usr/lib/gcc/x86_64-redhat- linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_iterator.h:634: note: candidates are: __gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >& __gnu_cxx::__normal_iterator<GCVAsso<GCVString,   GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >::operator=(const __gnu_cxx::__normal_iterator<GCVAsso<GCVString, GCVString>::KeyNode*, std::vector<GCVAsso<GCVString, GCVString>::KeyNode, std::allocator<GCVAsso<GCVString, GCVString>::KeyNode> > >&) 
     make[2]: *** [CMakeFiles/GCVCore.dir/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.o] Error 1 
     make[1]: *** [CMakeFiles/GCVCore.dir/all] Error 2 

답변

3

원본 코드는 STL에 대해 작성되었으며 std::vector<T>::iterator은 원시 포인터이므로 NULL로 초기화 될 수 있습니다.

전체의 호환성을 위해

, C++ 11에서

Viterator pCurrent = Viterator(); 

에 줄을 변경, 당신은이 Viterator 단순히 벌거 벗은 포인터가 될 수 있다는 것을 여기에 의미 완벽한 호환성으로

Viterator pCurrent{}; 

을 사용할 수 있습니다 . 이 경우 명시 적으로 기본 생성 값으로 설정하면 NULL로 설정됩니다. 아래는 간단한 예제입니다.

#include <iostream> 

typedef void * Iterator; 

int main(int, char**) 
{ 
    Iterator v1, v2=Iterator(); 
    std::cout << "uninitialized pointer: " << v1 << "\ninitialized pointer: " << v2 << std::endl; 
} 

출력은 : 그것은 pCurrent에 아무것도 않는 경우 프로그램이 여전히 그에게 새로운 값을 할당 이외의 다른 잘못 될 수 있음을

uninitialized pointer: 0x7fff5fc01052 
initialized pointer: 0 

주 (그 자체에 대해 비교하는 것이 유효 할 것 또는 그것을 복사하여 초기화 된 다른 반복자가 아닌 비 특이 반복자 또는 별도로 기본값으로 구성된 반복자를 비교하면 정의되지 않음).

+1

@Kuba : 반복자 유형이 참으로 포인터 인 경우, 초기화되지 않은 상태로 남겨두기 만합니다. C++ 11에서는'Viterator pCurrent {};'를 사용할 수 있지만 C++ 03에서는 할 수 없습니다. – Xeo

+0

제오 : 네 말이 맞아 !! 그걸 가져 주셔서 고마워요. 솔직히 말해서, 나는 그런 디폴트 타입을 만들 필요가 전혀 없었습니다. 그렇게 시도해 본 적은 결코 없었습니다. –

+0

@ Xeo : 이것은 단지 하나의 믿을 수없는 사이트임을 보여줍니다. 나는 한 달 내내 배웠다. 다시 한 번 감사드립니다. 누군가가 내가 모르는 것을 지적하면 항상 * 감사합니다. 나에게 더 많은 힘을;) –

0

반복자는이 시대의 가장이하는 것을 목적은 숫자가 아닌 :

  template <class TP> TP *GCVVector<TP>::Find(const TP &Obj) 
     { 
     #ifdef WIN32 
       using namespace std; 
       typedef typename vector<TP>::iterator Viterator; 
     #else 
     #ifdef __HP_aCC 
       using namespace std; 
       typedef typename vector<TP>::iterator Viterator; 
     #else 
       using namespace std; 
       typedef typename std::vector<TP>::iterator Viterator; 
     #endif 
     #endif 

       Viterator pCurrent =NULL ; 

내가 점점 오전 오류가 이것이다 : 여기 내가 오류가 무엇입니까 코드 샘플입니다 수를 생성자 매개 변수로 사용하지 않습니다.

그냥 현재 코드가하는 같은 방법으로 missbehave 것

Viterator pCurrent = Viterator(); 

으로 라인을 교체합니다.

+1

포인터 또는 POD 구조체 인 경우 반복기가 초기화되지 않은 상태로 남을 수 있습니다. – Xeo

+0

@Xeo 제대로 초기화하려면'{}'이 필요하다고 말합니다. 구조체에서도 C++ 03에서는'()'도 충분하지 않겠는가? 아니면 C++ 11에서 처음 소개 된 초기화 보증입니까? – RedX

+0

'type name();'은 C++에서 [most vexing parse] (http://stackoverflow.com/q/180172/500104)로도 알려져 있습니다. 이것은 실제로 함수를 선언합니다. ;) – Xeo

관련 문제