左操作数的类型为 'std::stringstream (__cdecl *)(std::string)'

left operand has type 'std::stringstream (__cdecl *)(std::string)'

本文关键字:std 操作数 string cdecl 类型 stringstream      更新时间:2023-10-16

两个代码的差异:

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)};
相关文章: