Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:在‘;之前需要构造函数、析构函数或类型转换&’;代币_C++_G++ - Fatal编程技术网

C++ 错误:在‘;之前需要构造函数、析构函数或类型转换&’;代币

C++ 错误:在‘;之前需要构造函数、析构函数或类型转换&’;代币,c++,g++,C++,G++,我写了以下代码。问题应该是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 //th

我写了以下代码。问题应该是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);
}
更改:

return istringstream&;
致:

但是,如果不使用返回值,则可以将
read()
的返回类型更改为
void
,而不返回任何内容。

更改:

return istringstream&;
致:


但是,如果不使用返回值,则可以将
read()
的返回类型更改为
void
,而不返回任何内容。

所有这些内容都在
std::
命名空间中定义


如果你懒惰,你可以
使用名称空间std,但这不是一个好的做法。

所有这些内容都在
std::
名称空间中定义


如果你懒惰,你可以
使用名称空间std,但这不是一个好的做法。

更改为“返回输入”后,仍然会出现相同的错误。我正在尝试编写一个返回istringstream的istringstream函数。只是为了练习。所以我想知道我做错了什么。感谢更改为“返回输入”后,仍然出现相同的错误。我正在尝试编写一个返回istringstream的istringstream函数。只是为了练习。所以我想知道我做错了什么。感谢上帝,它成功了,它是名称空间。此外,cin不是istringstream&。这也是问题所在。“input.eof()”检查将删除字符串的最后一个字。即,输入字符串“hello world”,输出“hello_3;”。为什么会发生这种情况?切换eof和getline。不要用逗号,太可怕了。太好了,它起作用了,它是名称空间。此外,cin不是istringstream&。这也是问题所在。“input.eof()”检查将删除字符串的最后一个字。即,输入字符串“hello world”,输出“hello_3;”。为什么会发生这种情况?切换eof和getline。不要用逗号,这太可怕了。请不要在其他人回答完问题后,编辑问题以完全改变其含义。这只会使答案变得难以理解,并使它们对未来的访问者毫无价值。您可以:(a)编辑您的问题并将新的相关问题附加到原始问题,或者(b)提出单独的问题。我已回滚您的编辑。没问题。:)尽管如此,还是发布你的第二个问题吧!请不要在其他人回答后编辑问题以完全改变其含义。这只会使答案变得难以理解,并使它们对未来的访问者毫无价值。您可以:(a)编辑您的问题并将新的相关问题附加到原始问题,或者(b)提出单独的问题。我已回滚您的编辑。没问题。:)尽管如此,还是发布你的第二个问题吧!
return input;