下面是我用c ++编写的一些代码。
ifstream objfile(s1.c_str());
string s2 = strfile;
std::istringstream objstr(s2);
if (path.empty()) objfile.set_rdbuf(objstr.rdbuf());
在Visual Studio C ++ 2017中编译没有问题。当我尝试在Redhat Linux中使用g ++编译时,它有错误
error: ‘std::ifstream’ has no member named ‘set_rdbuf’
编辑:基于对该问题的初步答案,我尝试将对set_rdbuf
的呼叫替换为
objfile.rdbuf(objstr.rdbuf());
这仍然不起作用。它说
rdbuf() const, note: candidate expects 0 arguments, 1 provided.
完整的错误消息真的很长,我删除了...中包含的In文件中的行。
error: no matching function for call to ‘std::basic_ifstream<char>::rdbuf(std::basic_istringstream<char>::__stringbuf_type*)’
if(path.empty()) objfile.rdbuf(objstr.rdbuf());
^
/usr/include/c++/4.8.2/fstream:509:7: note: std::basic_ifstream<_CharT, _Traits>::__filebuf_type* std::basic_ifstream<_CharT, _Traits>::rdbuf() const [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ifstream<_CharT, _Traits>::__filebuf_type = std::basic_filebuf<char>]
rdbuf() const
^
error: no matching function for call to ‘std::basic_ifstream<char>::rdbuf(std::basic_istringstream<char>::__stringbuf_type*)’
if (path.empty()) objfile.rdbuf(objstr.rdbuf());
^
/usr/include/c++/4.8.2/fstream:509:7: note: std::basic_ifstream<_CharT, _Traits>::__filebuf_type* std::basic_ifstream<_CharT, _Traits>::rdbuf() const [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ifstream<_CharT, _Traits>::__filebuf_type = std::basic_filebuf<char>]
rdbuf() const
^
/usr/include/c++/4.8.2/fstream:509:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8.2/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::basic_istringstream<char>; _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Tp = std::basic_istringstream<char>]’:
/usr/include/c++/4.8.2/bits/alloc_traits.h:254:4: required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::basic_istringstream<char>; _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Alloc = std::allocator<std::basic_istringstream<char> >; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8.2/bits/alloc_traits.h:393:57: required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::basic_istringstream<char>; _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Alloc = std::allocator<std::basic_istringstream<char> >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8.2/bits/vector.tcc:97:40: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Tp = std::basic_istringstream<char>; _Alloc = std::allocator<std::basic_istringstream<char> >]’
/usr/include/c++/4.8.2/bits/stl_vector.h:920:36: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::basic_istringstream<char>; _Alloc = std::allocator<std::basic_istringstream<char> >; std::vector<_Tp, _Alloc>::value_type = std::basic_istringstream<char>]’
/home/research/QZ/6_compile_Agency_Model/Agency_files/BondModel/utility.h:2413:37: required from here
/usr/include/c++/4.8.2/ext/new_allocator.h:120:4: error: use of deleted function ‘std::basic_istringstream<char>::basic_istringstream(const std::basic_istringstream<char>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
In file included from /usr/include/c++/4.8.2/complex:45:0,
from /home/research/boost_library/boost_1_68_0/boost/detail/container_fwd.hpp:98,
from /home/research/boost_library/boost_1_68_0/boost/container_hash/extensions.hpp:22,
from /home/research/boost_library/boost_1_68_0/boost/container_hash/hash.hpp:760,
from /home/research/boost_library/boost_1_68_0/boost/type_index/stl_type_index.hpp:42,
from /home/research/boost_library/boost_1_68_0/boost/type_index.hpp:29,
from /home/research/boost_library/boost_1_68_0/boost/any.hpp:20,
from /home/research/boost_library/boost_1_68_0/boost/program_options/value_semantic.hpp:12,
from /home/research/boost_library/boost_1_68_0/boost/program_options/options_description.hpp:13,
/usr/include/c++/4.8.2/sstream:272:11: note: ‘std::basic_istringstream<char>::basic_istringstream(const std::basic_istringstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_istringstream : public basic_istream<_CharT, _Traits>
^
/usr/include/c++/4.8.2/sstream:272:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.8.2/istream:58:11: note: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_istream : virtual public basic_ios<_CharT, _Traits>
^
/usr/include/c++/4.8.2/istream:58:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.8.2/bits/basic_ios.h:66:11: note: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_ios : public ios_base
^
/usr/include/c++/4.8.2/bits/ios_base.h:786:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
ios_base(const ios_base&);
^
/usr/include/c++/4.8.2/bits/basic_ios.h:66:11: error: within this context
class basic_ios : public ios_base
^
/usr/include/c++/4.8.2/sstream:272:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
class basic_istringstream : public basic_istream<_CharT, _Traits>
^
/usr/include/c++/4.8.2/sstream:272:11: error: use of deleted function ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’
/usr/include/c++/4.8.2/sstream:64:11: note: ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
^
/usr/include/c++/4.8.2/streambuf:802:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
basic_streambuf(const basic_streambuf& __sb)
^
/usr/include/c++/4.8.2/sstream:64:11: error: within this context
class basic_stringbuf : public basic_streambuf<_CharT, _Traits>
^
In file included from /usr/include/c++/4.8.2/vector:62:0,
/usr/include/c++/4.8.2/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::basic_istringstream<char>; _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}]’:
/usr/include/c++/4.8.2/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<std::basic_istringstream<char>*>; _ForwardIterator = std::basic_istringstream<char>*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8.2/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<std::basic_istringstream<char>*>; _ForwardIterator = std::basic_istringstream<char>*]’
/usr/include/c++/4.8.2/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<std::basic_istringstream<char>*>; _ForwardIterator = std::basic_istringstream<char>*; _Tp = std::basic_istringstream<char>]’
/usr/include/c++/4.8.2/bits/stl_uninitialized.h:281:69: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = std::basic_istringstream<char>*; _ForwardIterator = std::basic_istringstream<char>*; _Allocator = std::allocator<std::basic_istringstream<char> >]’
/usr/include/c++/4.8.2/bits/vector.tcc:415:43: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Tp = std::basic_istringstream<char>; _Alloc = std::allocator<std::basic_istringstream<char> >]’
/usr/include/c++/4.8.2/bits/vector.tcc:101:54: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::basic_istringstream<char, std::char_traits<char>, std::allocator<char> >}; _Tp = std::basic_istringstream<char>; _Alloc = std::allocator<std::basic_istringstream<char> >]’
/usr/include/c++/4.8.2/bits/stl_vector.h:920:36: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::basic_istringstream<char>; _Alloc = std::allocator<std::basic_istringstream<char> >; std::vector<_Tp, _Alloc>::value_type = std::basic_istringstream<char>]’
/home/research/QZ/6_compile_Agency_Model/Agency_files/BondModel/utility.h:2413:37: required from here
/usr/include/c++/4.8.2/bits/stl_construct.h:75:7: error: use of deleted function ‘std::basic_istringstream<char>::basic_istringstream(const std::basic_istringstream<char>&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
make[2]: *** [CMakeFiles/BondModel.dir/cashflowengine.cpp.o] Error 1
make[1]: *** [CMakeFiles/BondModel.dir/all] Error 2
答案 0 :(得分:1)
std::ifstream::set_rdbuf
受保护。 MSVC很有可能在std::ifstream
中将其公开为公共成员函数,这将解释您的代码为何在此处编译。
您想要的是rdbuf
:
std::basic_streambuf<CharT, Traits>* rdbuf( std::basic_streambuf<CharT, Traits>* sb );
将关联的流缓冲区设置为sb。通过调用clear()清除错误状态。返回操作之前的关联流缓冲区。如果没有关联的流缓冲区,则返回空指针。
答案 1 :(得分:1)
这很有趣。正如其他人指出的那样,这里的最初问题是set_rdbuf
成员函数受到保护,因此您不能直接调用它。当您报告调用正确的成员函数rdbuf
未被编译时感到很惊讶,因为该成员函数确实存在并且是公共的。
简短的版本是,我相信以下代码可以完成您想要的工作:
if (path.empty()) objfile.istream::rdbuf(objstr.rdbuf());
此处需要istream::
前缀的原因与C ++如何在派生类中命名查找有关。如果派生类声明了具有给定名称的成员函数,而您尝试调用具有该名称的函数,则编译器将不会寻找基类来查找该函数的潜在重载。在C ++ 11中,ifstream
类型添加了一个名为rdbuf
的新辅助函数,该函数将基础缓冲区作为filebuf*
返回。这遮盖了设置基础缓冲区的istream
函数rdbuf
。结果,如果调用rdbuf
的单参数版本,则C ++将找不到它,因为一旦找到rdbuf
中定义的零参数ifstream
,C ++就会停止搜索。添加显式的istream::rdbuf
调用会告诉编译器首先在istream
中搜索此函数,最终在该处找到所需的函数。
希望这会有所帮助!
答案 2 :(得分:0)
set_rdbuf是受保护的成员函数。这就是为什么它不可访问的原因。