//为什么这样工作?
// why does this work?
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream inputStream("scores.txt"); string line; getline(inputStream, line); cout << line; return 0; }//但是会因编译器错误而失败。
// but this fails with compiler errors.
#include <iostream> #include <fstream> #include <string> using namespace std; ifstream GetStream() { ifstream inputStream("scores.txt"); return inputStream; } int main() { ifstream inputStream = GetStream(); string line; getline(inputStream, line); cout << line; return 0; }在mac osx 10.6 w / g ++上编译4.2.1
compile on mac osx 10.6 w/ g++ 4.2.1
georges-iMac:cs106b george $ g ++ help.cpp -o help /usr/include/c++/4.2.1/bits/ios_base.h:在复制构造函数'std :: basic_ios> :: basic_ios(const std :: basic_ios>&)': /usr/include/c++/4.2.1/bits/ios_base.h:779:error:'std :: ios_base: :ios_base(const std :: ios_base&)'是private /usr/include/c++/4.2.1/iosfwd:55:错误:在此上下文中 /usr/include/c++/4.2。 1 / iosfwd:在复制构造函数'std :: basic_ifstream> :: basic_ifstream(const std :: basic_ifstream>&)': /usr/include/c++/4.2.1/iosfwd:89:方法'std :: basic_ios> :: basic_ios(const std :: basic_ios>&)'首先需要这里 /usr/include/c++/4.2.1/streambuf:在复制构造函数'std :: basic_filebuf> :: basic_filebuf(const std :: basic_filebuf>&)': /usr/include/c++/4.2.1/streambuf:794:error:'std :: basic_streambuf< _CharT,_Traits> :: basic_streambuf const std :: basic_streambuf< _CharT,_Traits>&)[with _CharT = char,_Traits = std :: char_traits]'是私有的 /usr/include/c++/4.2.1/iosfwd:86:在这个上下文中 /usr/include/c++/4.2.1/iosfwd:在复制构造函数'std :: basic_ifstream> :: basic_ifstream(const std :: basic_ifstream>&)': / usr /include/c++/4.2.1/iosfwd:89:note:合成方法'std :: basic_filebuf> :: basic_filebuf(const std :: basic_filebuf>&)'首先需要这里 help.cpp:In function 'std :: ifstream GetStream()': help.cpp:9:note:合成方法'std :: basic_ifstream> :: basic_ifstream(const std :: basic_ifstream>&)' >
georges-iMac:cs106b george$ g++ help.cpp -o help /usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios >::basic_ios(const std::basic_ios >&)’: /usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private /usr/include/c++/4.2.1/iosfwd:55: error: within this context /usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’: /usr/include/c++/4.2.1/iosfwd:89: note: synthesized method ‘std::basic_ios >::basic_ios(const std::basic_ios >&)’ first required here /usr/include/c++/4.2.1/streambuf: In copy constructor ‘std::basic_filebuf >::basic_filebuf(const std::basic_filebuf >&)’: /usr/include/c++/4.2.1/streambuf:794: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits]’ is private /usr/include/c++/4.2.1/iosfwd:86: error: within this context /usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’: /usr/include/c++/4.2.1/iosfwd:89: note: synthesized method ‘std::basic_filebuf >::basic_filebuf(const std::basic_filebuf >&)’ first required here help.cpp: In function ‘std::ifstream GetStream()’: help.cpp:9: note: synthesized method ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’ first required here
推荐答案问题出在这个函数:
ifstream GetStream() { ifstream inputStream("scores.txt"); return inputStream; }您要传回 > by value 这是不允许的,因为它需要复制本地对象,但是你不能复制stream对象。通过将复制构造函数设为私有,可以禁止在C ++中复制任何流。任何方式任何,无论是 stringstream , istream , ostream , iostream 或任何。阅读我的帖子这里为其残疾人的理由:
You're returning a stream object by value which is not allowed, because it requires copying of the local object, but you cannot make copy of stream object. Copying of any stream in C++ is disabled by having made the copy constructor private. Any means ANY, whether it is stringstream, istream, ostream, iostream or whatever. Read my post here for the rationale why its disabled:
- 为什么不允许复制stringstream?
- Why copying stringstream is not allowed?
可能希望通过引用返回它,但是您也不能通过引用返回它,因为 inputStream 是本地对象。
Now, you may want to return it by reference, but you cannot return it by reference also, because inputStream is a local object.
您可以尝试:
ifstream* GetStream() { return new ifstream("scores.txt"); } int main() { ifstream *inputStream = GetStream(); if ( inputStream && *inputStream ) { string line; getline(*inputStream, line); cout << line; } if ( inputStream ) delete inputStream; return 0; }但我想知道为什么你会这样做。为什么不把一切都封装在一个类中?
But I'm wondering why you would do that. Why not encapsulate everything in a class?
更多推荐
c ++ getline从函数调用时编译错误
发布评论