提问人:lucd 提问时间:5/4/2023 最后编辑:Alan Birtleslucd 更新时间:5/5/2023 访问量:62
使用旧的 GCC 分配 ifstream 对象 - 此处首先需要合成方法 std::basic_ifstream
Assign ifstream object with old GCC - synthesized method std::basic_ifstream first required here
问:
我想将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
[...]
答:
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[]
评论
ifstream
new
std::ifstream