我曾經在一個類中使用過下面的代碼片段。今天,我把它複製到另一個班。編譯時,我得到以下錯誤。奇怪的是,這樣的事實已經發生了!你能告訴一下有什麼事嗎?謝謝。stringstream轉換錯誤
我剛剛使用以下片段代碼在演示項目中進行測試。我像一個魅力跑!
int main()
{
char buffer[] = "I1 I2 V1 V2 I3 V3 I4 DO V4";
std::stringstream s(buffer);
std::istream_iterator<std::string> begin(s);
std::istream_iterator<std::string> end;
std::vector<std::string> IVector;
std::vector<std::string> VVector;
for (; begin != end; ++begin)
{
std::string sElem = *begin;
switch((*begin)[0])
{
case 'I':
IVector.push_back(sElem);
break;
case 'V':
VVector.push_back(sElem);
break;
default:
;
}
}
return 0;
}
void ClassifyChannel(char* szBuffer)
{
// Empty vectors
m_svIRChannels.clear();
m_svVISChannels.clear();
std::stringstream s(szBuffer);
std::istream_iterator<std::string> itBegin(s);
std::istream_iterator<std::string> itEnd;
for (; itBegin != itEnd; ++itBegin)
{
std::string sElem = *itBegin;
// Switch on first character
switch ((*itBegin)[0])
{
// Infrared channel, such as IR1, IR2, IR3 (WV), and IR4
case 'I':
case 'W':
// Insert into IR channel vector here
m_svIRChannels.push_back(sElem);
break;
// Visible channels, such as VIS, and VIS1KM
case 'V':
// Insert into VIS channel vector here
m_svVISChannels.push_back(sElem);
break;
}
}
}
的錯誤消息是
error C2440: 'initializing' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>f:\tips\tips\fy2hdf5dataiohandler.cpp(830) : error C2664: 'std::istream_iterator<_Ty>::istream_iterator(std::basic_istream<_Elem,_Traits> &)' : cannot convert parameter 1 from 'int' to 'std::basic_istream<_Elem,_Traits> &'
1> with
1> [
1> _Ty=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> and
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
此代碼似乎沒問題。你使用什麼stl? –
您提交的代碼缺少必需的標題。將頭添加回來,它編譯好。請發佈一個完整的重現問題的簡單示例。一般來說,代碼片段無法評估正確性。 –
@Mhhran:由於使用了'std'命名空間,他必須使用C++標準庫。他使用的stdlib的實現方式確實值得爭論! –