左操作数的类型为 'std::stringstream (__cdecl *)(std::string)'
left operand has type 'std::stringstream (__cdecl *)(std::string)'
两个代码的差异:
char buf[2048];
stringstream in(string(buf));
int tmpInt;
while ((in >> tmpInt)) { // wrong, error C2296: '>>' : illegal, left operand has type 'std::stringstream (__cdecl *)(std::string)'
}
和
char buf[2048];
string tmpStr(buf);
stringstream in(tmpStr);
while ((in >> tmpInt)) { // right
}
我认为他们做的是相同的事情:都使用字符串来构造字符串流对象。无论是临时对象还是实际对象,我们都会在stringstream中调用字符串复制构造函数(只复制buf内容)
IDE:vs2010
那么,这两种方式有什么不同呢?或字符串流实现方式。
谢谢。
Chris给出了答案。该代码等效于以下内容:
stringstream in(string buf);
在C++中,人们称之为最麻烦的解析。
编译器将其视为一个函数声明。in
是一个返回字符串流并接受string
作为参数的函数。请注意,编译器在错误消息std::stringstream (__cdecl *)(std::string)
中告诉您这一点。
您将需要一组额外的括号或C++11统一初始化器语法来告诉编译器它不是您正在声明的函数:
stringstream in((string(buf)));
stringstream in{string(buf)};
相关文章:
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 使用 std::move 将参数传递给函数,如果该参数声明为按值传递或使用移动操作数 &&,是否有区别?
- 二进制">>":未找到采用类型为"std::p air<int,int>"的右操作数的运算符
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 错误:C2679 二进制"==":未找到采用类型 'const std::string' 的右侧操作数的运算符(或者没有可接受的转换)
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 如何修复此错误:二进制表达式的操作数无效("std::vector<double>"和"double")?
- 没有运算符匹配这些操作数;操作数类型为: std::istream >> const char [5]
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 无效的操作数编译器错误涉及std :: quare_range
- 没有运算符"="匹配这些操作数操作数操作数类型为 std::basic_ostream<char、std::char_traits<char>> = int
- 没有运算符">>"匹配这些操作数操作数操作数类型为: std::istream >>双*
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 错误 C2678:二进制'==':找不到采用 'std::pair<const _Kty,_Ty>' 型左操作数的运算符
- std::map 插入错误:没有运算符"<"与这些操作数匹配
- 错误 C2678:二进制">>":找不到采用类型为"std::basic_istream<_Elem,_Traits>"的左操作数的运算符
- 使用 std::replace C++ 时二进制表达式的操作数无效