c ++ getline从函数调用时编译错误

编程入门 行业动态 更新时间:2024-10-20 03:23:49
本文介绍了c ++ getline从函数调用时编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

//为什么这样工作?

// 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从函数调用时编译错误

本文发布于:2023-10-24 14:07:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   getline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!