2013-05-11 3 views
1

나는 programm를 계속해야한다. 내 앞에 프로그래머는 구조를 많이 사용 :C++ std : 벡터 < T* const>

std:vector< T* const> 

그는 2010 ++ 비주얼 스튜디오 C에서 경악을 작성하고이를 컴파일 할 수 있었다. g ++을 사용하고 컴파일 오류가 발생합니다. 그는이 구조 원은 무엇

g++ -g -Wall -c -std=c++11 -pedantic -I/usr/include/SuperLU/ src/Cell.cpp -o obj/Cell.o 
In file included from src/Cell.cpp:13:0: 
src/Cell.h:81:2: warning: extra ';' [-pedantic] 
In file included from /usr/include/x86_64-linux-gnu/c++/4.7/./bits/c++allocator.h:34:0, 
       from /usr/include/c++/4.7/bits/allocator.h:48, 
       from /usr/include/c++/4.7/string:43, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Cell.cpp:2: 
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of 'struct __gnu_cxx::new_allocator<BattPackage::Leg* const>': 
/usr/include/c++/4.7/bits/allocator.h:89:11: required from 'class std::allocator<BattPackage::Leg* const>' 
/usr/include/c++/4.7/bits/alloc_traits.h:92:43: required from 'struct std::allocator_traits<std::allocator<BattPackage::Leg* const> >' 
/usr/include/c++/4.7/ext/alloc_traits.h:110:10: required from 'struct __gnu_cxx::__alloc_traits<std::allocator<BattPackage::Leg* const> >' 
/usr/include/c++/4.7/bits/stl_vector.h:76:28: required from 'struct std::_Vector_base<BattPackage::Leg* const, std::allocator<BattPackage::Leg* const> >' 
/usr/include/c++/4.7/bits/stl_vector.h:208:11: required from 'class std::vector<BattPackage::Leg* const>' 
src/Cell.h:283:39: required from here 
/usr/include/c++/4.7/ext/new_allocator.h:83:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = BattPackage::Leg* const; __gnu_cxx::new_allocator<_Tp>::const_pointer = BattPackage::Leg* const*; __gnu_cxx::new_allocator<_Tp>::const_reference = BattPackage::Leg* const&]' cannot be overloaded 
/usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = BattPackage::Leg* const; __gnu_cxx::new_allocator<_Tp>::pointer = BattPackage::Leg* const*; __gnu_cxx::new_allocator<_Tp>::reference = BattPackage::Leg* const&] 

그는 추가하고 포인터를 제거하고 포인터의 대상을 조작 만있는 포인터 포인트 개체를 변경할 수 없습니다 포인터의 벡터이다.

T * const는 할당 할 수 없기 때문에 컴파일해서는 안된다.

누구나 Visual Studio에서 컴파일되는 이유를 알고 있습니까? 전체 코드를 변경할 필요없이 선언에서이를 대체 할 수 있습니까?

답변

8

std::vector은 할당자를 사용하여 구성원에게 메모리를 할당, 재 할당 및 할당 해제합니다. Allocator 요구 사항은 T 유형의 Allocator X에 대해서만 정의되며 여기서 T은 "비 const의 비 참조 객체 유형"(C++ 11 표 27)입니다. 그러므로 std::vector<T* const>을 사용하면 할당자가 const 요소 유형에 정의되어 있지 않으므로 정의되지 않은 동작이 발생합니다.

가능한 빨리 수정하는 것이 좋습니다. 신고 내용을 std::vector<T*>으로 변경하면 많은 문제가 발생하지 않을 것입니다.

+0

그럼 VS 컴파일러가이 문제를 감지 할 수없는 이유는 무엇입니까? 컴파일러 버그입니까? – shivakumar

+0

@shivakumar 정의되지 않은 동작입니다. 아무거나 일어날 수있다. 그것은 그것을 조용히 무시하는 버그가 아닙니다. –

+0

그건 내 생각에 효과가있는 Ok. 그래서 VS가 std :: vector로 가정한다고 생각하십니까? ? – MagunRa

관련 문제