【问题标题】:Cannot convert Boost path iterators to strings无法将 Boost 路径迭代器转换为字符串
【发布时间】:2012-02-29 00:16:47
【问题描述】:

我正在尝试从nmdepend 获取以下代码进行编译

const std::string Bfd::packageName(const fs::path& path, int packageLevel)
{
  fs::path::iterator p = path.end();
  --p;

  for(int i = 0; i < packageLevel; ++i)
      --p;

  return *p;
}

但是它正在生成以下编译器错误

/Users/nick/Software/nmdepend/src/Bfd.cpp: In static member function ‘static const std::string Bfd::packageName(const boost::filesystem3::path&, int)’:
/Users/nick/Software/nmdepend/src/Bfd.cpp:27: error: conversion from ‘const boost::filesystem3::path’ to non-scalar type ‘const std::string’ requested

应该如何修改这段代码,以便返回一个字符串,同时保持使用迭代器尝试的操作?

【问题讨论】:

  • 不应该是"return (*p).string();" ?

标签: c++ boost stl compiler-errors boost-filesystem


【解决方案1】:

path 不能隐式转换为字符串。不过这应该可行:

return p->string();

【讨论】:

  • 或者可能是 p->native()。看起来他们已经改变了很多。 OP,通读boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html
  • @Duck:返回的类型会因环境而异。它会在 Windows 上返回 std::basic_string&lt;wchar_t&gt;,在 POSIX 系统上返回 std::basic_string&lt;char&gt;string 函数进行必要的转换以将其转换为 std::string
猜你喜欢
  • 1970-01-01
  • 2012-11-23
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多