2010-05-16 2 views
8

char 배열에서 아래와 같은 것을 사용하려하지만 컴파일되지 않습니다. 그러나 짧은 []가있는 예제는 잘 동작합니다. 왜 그런가? :) 당신은 컴파일 오류가 발생 <boost/foreach.hpp>의 라인에 가면부스트 ForEach 질문

char someChars[] = {'s','h','e','r','r','y'}; 
    BOOST_FOREACH(char& currentChar, someChars) 
    { 

    } 


short array_short[] = { 1, 2, 3 }; 
    BOOST_FOREACH(short & i, array_short) 
    { 
     ++i; 
    } 

답변

17

, 당신은 다음과 같은 주석이 표시됩니다

// **** READ THIS IF YOUR COMPILE BREAKS HERE **** 
// 
// There is an ambiguity about how to iterate over arrays of char and wchar_t. 
// Should the last array element be treated as a null terminator to be skipped, or 
// is it just like any other element in the array? To fix the problem, you must 
// say which behavior you want. 
// 
// To treat the container as a null-terminated string, merely cast it to a 
// char const *, as in BOOST_FOREACH(char ch, (char const *)"hello") ... 
// 
// To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>, 
// as in BOOST_FOREACH(char ch, boost::as_array("hello")) ... 

하여 컴파일 오류를 수정해야 주석과 같이 boost::as_array(someChars)을 사용합니다.

+0

많은 감사. 너무 빨리 게시 할 수있었습니다. :) – bobber205