2010-05-16 7 views
8

एक चार सरणी के साथ नीचे की तरह कुछ उपयोग करने की कोशिश कर रहा है लेकिन यह संकलित नहीं करता है। लेकिन संक्षिप्त [] के साथ उदाहरण ठीक काम करता है। किसी भी विचार क्यों? :)बूस्ट फॉरएच प्रश्न

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

आप <boost/foreach.hpp> में लाइन है कि संकलन त्रुटि को जन्म देती है के लिए जाना है, तो आप निम्नलिखित टिप्पणी देखेंगे:

// **** 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

संबंधित मुद्दे