字符串流转换错误
stringstream conversion error
我曾经在一个类中使用过以下代码片段。今天,我把它复制到另一个班上。编译时,我出现了以下错误。真奇怪竟然发生了这样的事!你能告诉我怎么回事吗?非常感谢。
我刚刚使用以下代码片段在一个演示项目中进行了测试。我跑得很有魅力!
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:tipstipsfy2hdf5dataiohandler.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> ]
您使用的是什么编译器?你的代码片段在我的盒子上用VC10编译得很好。你忘了包括所有正确的头文件了吗?对于这段代码,你需要这3个标题:
#include <string>
#include <sstream>
#include <iterator>
由于VC10、<iterator>
不被其他人隐式地包括。你需要自己把它包括在内。
我的猜测;你没有明确地做
#include <string>
IO流头只有std::string
的前向声明,但这在您的情况下是不够的。尝试显式地包含string
标头来解决此问题。
#include <sstream>
需要包含。
我在VS2017和VS2019中尝试过,它通过包含此标头来工作。
以下是我尝试过的示例代码。
int main()
{
string line = "asasas asasas asasas asasas must try";
stringstream check1(line);
}
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- C++使用params创建线程函数会导致转换错误
- 在混合代码库中将C转换为C++时出现许多包含错误
- 为什么g++在未执行的代码处标记强制转换错误
- 错误:从"int"到枚举c++的转换无效
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 从标准::未来<void>到非标量标准::未来<bool>引发的错误转换
- 使用宏的错误转换
- 在 opencv 中使用 Color_YUV2BGR 从 YUV 到 BGR 的错误转换
- 无法通过此操作,因为类型的错误转换
- PyQt5:使用自定义信号时 int 对象的错误转换
- 从长途错误转换
- C 错误:转换为非量表类型
- C 错误:转换为执行字符集
- 使用SOCI从PostgreSQL数据库获取数据时发生错误转换
- 分段错误C++转换问题
- 在ARM上的Qt中从双精度到QString的错误转换
- C++浮点转换为Python浮点错误转换
- 错误:转换为非标量类型