gpt4 book ai didi

c++ - 错误 : expected constructor, 析构函数,或 ‘&’ token 之前的类型转换

转载 作者:行者123 更新时间:2023-11-30 01:28:01 25 4
gpt4 key购买 nike

我写了以下代码。问题应该是 istringstream 函数。我做错了什么?提前致谢。

//read a string from input with a istringstream function and output the string   
word by word;
//1.the function takes and returns an istringstream&
//2.the function reads the stream until it hits eof
//the function should print the contents of an istringstream object
#include <iostream>
#include <string>
#include <sstream>

istringstream& read(istringstream& input)
{

string string, word;
while(getline(input,string), !input.eof())
{
if (input)
{
istringstream instring(string);
instring>>word;
cout<<word<<'_'<<ends;
}
if (input.bad())
throw runtime_error("data is corrupted");
if (input.fail())
cerr<<"data failed, try again"<<ends;
input.close();
input.clear();
}
return istringstream&;
}

int main ()
{
cout<<"enter a string"<<endl;
read(cin);
}

这个输出错误是:(第9行)

 error: expected constructor, destructor, or type conversion before ‘&’ token

最佳答案

改变:

return istringstream&;

到:

return input;

但是假设您不使用返回值,您可以将 read() 的返回类型更改为 void 并且什么都不返回。

关于c++ - 错误 : expected constructor, 析构函数,或 ‘&’ token 之前的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8054791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com