错误:预期在 ‘&’ 之前进行构造函数、析构函数或类型转换代币
我写了以下代码。问题应该是istingstream函数。我做错了什么?提前致谢。
//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
I wrote the following codes. The problem should be the istringstream function. what did I do wrong? Thanks in advance.
//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);
}
this output error is: (line 9)
error: expected constructor, destructor, or type conversion before ‘&’ token
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为:
但是,如果您不使用返回值,则可以将
read()
的返回类型更改为void
并且根本不返回任何内容。Change:
to:
However given you don't use the returned value you could change the return type of
read()
tovoid
and return nothing at all.所有这些东西都在
std::
命名空间中定义。如果你很懒,你可以
using namespace std;
,但这不是一个好的做法。All those things are defined in the
std::
namespace.If you are lazy you can
using namespace std;
, but it's not a good practice.