boost::filesystem::path::append (通过迭代器)导致编译器错误

发布于 2024-12-09 21:31:05 字数 4393 浏览 1 评论 0原文

我正在尝试使用 boost::filesystem 生成新路径,如下所示

#include <iostream>
#include <string>
#include <boost\filesystem.hpp>

namespace bf = boost::filesystem;

bf::path o("C:\\mir");
bf::path p("C:\\mir\\dir\\beer.txt");
bf::path r("C:\\test");

auto pp = std::mismatch(o.begin(), o.end(), p.begin());
r.append(pp.second, p.end()); // this line causes compiler error

我收到编译器错误,如下所示

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2885): warning C4996: 'std::_Mismatch1': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
      c:\vc\include\xutility(2868) : see declaration of 'std::_Mismatch1'
      g:\copywithpath\copywithpath.cpp(40) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::mismatch<boost::filesystem3::path::iterator,boost::filesystem3::path::iterator>(_InIt1,_InIt1,_InIt2)' being compiled
      with
      [
          _Ty1=boost::filesystem3::path::iterator,
          _Ty2=boost::filesystem3::path::iterator,
          _InIt1=boost::filesystem3::path::iterator,
          _InIt2=boost::filesystem3::path::iterator
      ]
c:\vc\include\xstring(506): error C2621: member 'std::_String_val<_Elem,_Alloc>::_Bxty::_Buf' of union 'std::_String_val<_Elem,_Alloc>::_Bxty' has copy constructor
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      c:\vc\include\xstring(509) : see reference to class template instantiation 'std::_String_val<_Elem,_Alloc>::_Bxty' being compiled
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      c:\vc\include\xstring(522) : see reference to class template instantiation 'std::_String_val<_Elem,_Alloc>' being compiled
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      f:\boost\filesystem\v3\path.hpp(621) : see reference to class template instantiation 'std::basic_string<_Elem>' being compiled
      with
      [
          _Elem=boost::filesystem3::path
      ]
      f:\boost\filesystem\v3\path.hpp(232) : see reference to function template instantiation 'boost::filesystem3::path &boost::filesystem3::path::append<InputIterator>(InputIterator,InputIterator,const boost::filesystem3::path::codecvt_type &)' being compiled
      with
      [
          InputIterator=boost::filesystem3::path::iterator
      ]
      g:\copywithpath\copywithpath.cpp(42) : see reference to function template instantiation 'boost::filesystem3::path &boost::filesystem3::path::append<_Ty2>(InputIterator,InputIterator)' being compiled
      with
      [
          _Ty2=boost::filesystem3::path::iterator,
          InputIterator=boost::filesystem3::path::iterator
      ]
f:\boost\filesystem\v3\path.hpp(622): error C2665: 'boost::filesystem3::path_traits::convert' : none of the 8 overloads could convert all the argument types
     f:\boost\filesystem\v3\path_traits.hpp(96): could be 'void boost::filesystem3::path_traits::convert(const char *,const char *,std::wstring &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(102): or       'void boost::filesystem3::path_traits::convert(const wchar_t *,const wchar_t *,std::string &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(130): or       'void boost::filesystem3::path_traits::convert(const char *,const char *,std::string &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(150): or       'void boost::filesystem3::path_traits::convert(const wchar_t *,const wchar_t *,std::wstring &,const boost::filesystem3::path_traits::codecvt_type &)'
     while trying to match the argument list '(const boost::filesystem3::path *, const boost::filesystem3::path *, boost::filesystem3::path::string_type, const boost::filesystem3::path::codecvt_type)'

我的代码有什么问题以及如何修复它。提前致谢。

I'm trying to generate new path using boost::filesystem as follows

#include <iostream>
#include <string>
#include <boost\filesystem.hpp>

namespace bf = boost::filesystem;

bf::path o("C:\\mir");
bf::path p("C:\\mir\\dir\\beer.txt");
bf::path r("C:\\test");

auto pp = std::mismatch(o.begin(), o.end(), p.begin());
r.append(pp.second, p.end()); // this line causes compiler error

I get compiler error as follows

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2885): warning C4996: 'std::_Mismatch1': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
      c:\vc\include\xutility(2868) : see declaration of 'std::_Mismatch1'
      g:\copywithpath\copywithpath.cpp(40) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::mismatch<boost::filesystem3::path::iterator,boost::filesystem3::path::iterator>(_InIt1,_InIt1,_InIt2)' being compiled
      with
      [
          _Ty1=boost::filesystem3::path::iterator,
          _Ty2=boost::filesystem3::path::iterator,
          _InIt1=boost::filesystem3::path::iterator,
          _InIt2=boost::filesystem3::path::iterator
      ]
c:\vc\include\xstring(506): error C2621: member 'std::_String_val<_Elem,_Alloc>::_Bxty::_Buf' of union 'std::_String_val<_Elem,_Alloc>::_Bxty' has copy constructor
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      c:\vc\include\xstring(509) : see reference to class template instantiation 'std::_String_val<_Elem,_Alloc>::_Bxty' being compiled
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      c:\vc\include\xstring(522) : see reference to class template instantiation 'std::_String_val<_Elem,_Alloc>' being compiled
      with
      [
          _Elem=boost::filesystem3::path,
          _Alloc=std::allocator<boost::filesystem3::path>
      ]
      f:\boost\filesystem\v3\path.hpp(621) : see reference to class template instantiation 'std::basic_string<_Elem>' being compiled
      with
      [
          _Elem=boost::filesystem3::path
      ]
      f:\boost\filesystem\v3\path.hpp(232) : see reference to function template instantiation 'boost::filesystem3::path &boost::filesystem3::path::append<InputIterator>(InputIterator,InputIterator,const boost::filesystem3::path::codecvt_type &)' being compiled
      with
      [
          InputIterator=boost::filesystem3::path::iterator
      ]
      g:\copywithpath\copywithpath.cpp(42) : see reference to function template instantiation 'boost::filesystem3::path &boost::filesystem3::path::append<_Ty2>(InputIterator,InputIterator)' being compiled
      with
      [
          _Ty2=boost::filesystem3::path::iterator,
          InputIterator=boost::filesystem3::path::iterator
      ]
f:\boost\filesystem\v3\path.hpp(622): error C2665: 'boost::filesystem3::path_traits::convert' : none of the 8 overloads could convert all the argument types
     f:\boost\filesystem\v3\path_traits.hpp(96): could be 'void boost::filesystem3::path_traits::convert(const char *,const char *,std::wstring &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(102): or       'void boost::filesystem3::path_traits::convert(const wchar_t *,const wchar_t *,std::string &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(130): or       'void boost::filesystem3::path_traits::convert(const char *,const char *,std::string &,const boost::filesystem3::path_traits::codecvt_type &)'
     f:\boost\filesystem\v3\path_traits.hpp(150): or       'void boost::filesystem3::path_traits::convert(const wchar_t *,const wchar_t *,std::wstring &,const boost::filesystem3::path_traits::codecvt_type &)'
     while trying to match the argument list '(const boost::filesystem3::path *, const boost::filesystem3::path *, boost::filesystem3::path::string_type, const boost::filesystem3::path::codecvt_type)'

What's wrong with my code and how I can fix it. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2024-12-16 21:31:05

path::append 需要一系列字符,您提供了一系列路径。您应该附加每个元素自己的元素:

auto pp = std::mismatch(o.begin(), o.end(), p.begin());
for(auto iter = pp.second; iter != p.end(); ++iter)
    r /= *iter;

不幸的是 boost::filesystem 路径不提供路径算术接口。

path::append expects a range of characters, you provided a range of paths. You should append each element by it's own:

auto pp = std::mismatch(o.begin(), o.end(), p.begin());
for(auto iter = pp.second; iter != p.end(); ++iter)
    r /= *iter;

Unfortunately boost::filesystem paths don't provide an interface for path arithmetics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文