使用旧的 GCC 分配 ifstream 对象 - 此处首先需要合成方法 std::basic_ifstream

Assign ifstream object with old GCC - synthesized method std::basic_ifstream first required here

提问人:lucd 提问时间:5/4/2023 最后编辑:Alan Birtleslucd 更新时间:5/5/2023 访问量:62

问:

我想将ifstream对象保存到映射中,以便以后读取它们。 我正在使用旧的 GCC 版本 4.4.7(C++11 不可用)并且无法编译我的代码。

这是我的代码示例:

#include <iostream>
#include <fstream>
#include <map>

int main()
{
    std::map<std::string, std::ifstream> DicoRCALbin;
    DicoRCALbin["a"] = std::ifstream("M3-6I_10.rcal_0001.bin", std::ios_base::binary);
}

错误消息:

$ g++ problem.cpp
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ios:39,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:40,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iostream:40,
                 from test.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h: In member function 'std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ios_base.h:793: erreur: 'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:47: erreur: à l'intérieur du contexte
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd: In member function 'std::basic_istream<char, std::char_traits<char> >& std::basic_istream<char, std::char_traits<char> >::operator=(const std::basic_istream<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:53: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)' first required here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd: In member function 'std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:81: note: synthesized method 'std::basic_istream<char, std::char_traits<char> >& std::basic_istream<char, std::char_traits<char> >::operator=(const std::basic_istream<char, std::char_traits<char> >&)' first required here 
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/streambuf: In member function 'std::basic_filebuf<char, std::char_traits<char> >& std::basic_filebuf<char, std::char_traits<char> >::operator=(const std::basic_filebuf<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/streambuf:778: erreur: 'std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:78: erreur: à l'intérieur du contexte
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd: In member function 'std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/iosfwd:81: note: synthesized method 'std::basic_filebuf<char, std::char_traits<char> >& std::basic_filebuf<char, std::char_traits<char> >::operator=(const std::basic_filebuf<char, std::char_traits<char> >&)' first required here 
test.cpp: In function 'int main()':
test.cpp:8: note: synthesized method 'std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)' first required here 
[...]
C++ IFSTREAM C++98

评论

2赞 Some programmer dude 5/4/2023
流不可复制。您正在尝试复制初始化流,但这是不可能实现的。
0赞 Marek R 5/4/2023
此代码需要 gcc 4.4.7 不支持的 C++11(移动语义)。
0赞 Petok Lorand 5/4/2023
如果移动不是一个选项,您可以创建一个指针容器,并在将实际对象添加到容器时使用它们。ifstreamnew
0赞 Sam Varshavchik 5/4/2023
当前版本的 C++ 具有可用于避免复制对象的功能,以及使用不可复制的对象。在当前版本的 C++ 中可以有一个可用的对象映射,不幸的是,您的 C++ 编译器太旧了,您对此无能为力。std::ifstream
0赞 Alan Birtles 5/5/2023
请不要为问题添加答案,如果您已经解决了自己的问题,请添加答案

答:

0赞 jrok 5/5/2023 #1

你有没有想过boost::ref库?

#include <fstream>
#include <map>
#include <boost/ref.hpp>

int main()
{
    std::map<std::string, boost::reference_wrapper<std::ifstream> > DicoRCALbin;
    std::ifstream ifs("", std::ios_base::binary);
    DicoRCALbin.insert(std::make_pair("a", boost::ref(ifs)));
}

你必须放弃,因为 ifstream 不是默认可构造的。 希望能有所帮助。operator[]