C++ Primer中第8章关于流的一道习题,帮忙看一下解决方法
C++ Primer中第8章关于流的一道习题,帮忙看一下
问题:编写一个函数,其唯一的形参和返回值都是istream&类型。这个函数应一直读取流直到到达文件结束符为止,还应将读到的内容输出到标准输出中。最后,重设流使其有效,并返回该流。通过以cin为实参实现掉用来测试这个函数。
这个是我写的程序:
#include <iostream>
#include <string>
using namespace std;
istream& ReadInput(istream& state)
{
string temp;
while(state>>temp, !state.eof())
cout<<temp;
state.clear();
return state;
}
int main()
{
istream _state = ReadInput(cin);
return 0;
}
编译时出现错误:
c:\program files\microsoft visual studio 9.0\vc\include\istream(846) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
这个是什么意思?
如果在main()函数中调用的时候直接调用就没有错误,为什么啊?
#include <iostream>
#include <string>
using namespace std;
istream& ReadInput(istream& state)
{
string temp;
while(state>>temp, !state.eof())
cout<<temp;
state.clear();
return state;
}
int main()
{
ReadInput(cin); //这里直接调用就可以。
return 0;
}
还有,我这个程序对不对啊?符不符合题目的要求啊?
------解决方案--------------------
istream& _state = ReadInput(cin);
------解决方案--------------------
没有错误阿
问题:编写一个函数,其唯一的形参和返回值都是istream&类型。这个函数应一直读取流直到到达文件结束符为止,还应将读到的内容输出到标准输出中。最后,重设流使其有效,并返回该流。通过以cin为实参实现掉用来测试这个函数。
这个是我写的程序:
#include <iostream>
#include <string>
using namespace std;
istream& ReadInput(istream& state)
{
string temp;
while(state>>temp, !state.eof())
cout<<temp;
state.clear();
return state;
}
int main()
{
istream _state = ReadInput(cin);
return 0;
}
编译时出现错误:
c:\program files\microsoft visual studio 9.0\vc\include\istream(846) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
这个是什么意思?
如果在main()函数中调用的时候直接调用就没有错误,为什么啊?
#include <iostream>
#include <string>
using namespace std;
istream& ReadInput(istream& state)
{
string temp;
while(state>>temp, !state.eof())
cout<<temp;
state.clear();
return state;
}
int main()
{
ReadInput(cin); //这里直接调用就可以。
return 0;
}
还有,我这个程序对不对啊?符不符合题目的要求啊?
------解决方案--------------------
istream& _state = ReadInput(cin);
------解决方案--------------------
没有错误阿