【问题标题】:boost filesystem version 3 issue?提升文件系统版本 3 问题?
【发布时间】:2011-05-20 15:06:35
【问题描述】:

boost 网站上的示例代码不起作用。 http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/tutorial.html#Using-path-decomposition

int main(int argc, char* argv[])
{
  path p (argv[1]);   // p reads clearer than argv[1] in the following code

  try
  {
    if (exists(p))    // does p actually exist?
    {
      if (is_regular_file(p))        // is p a regular file?   
        cout << p << " size is " << file_size(p) << '\n';

      else if (is_directory(p))      // is p a directory?
      {
        cout << p << " is a directory containing:\n";

        typedef vector<path> vec;             // store paths,
        vec v;                                // so we can sort them later

        copy(directory_iterator(p), directory_iterator(), back_inserter(v));

        sort(v.begin(), v.end());             // sort, since directory iteration
                                          // is not ordered on some file systems

        for (vec::const_iterator it (v.begin()); it != v.end(); ++it)
        {
          path fn = it->path().filename();   // extract the filename from the path
          v.push_back(fn);                   // push into vector for later sorting
        }
      }

      else
        cout << p << " exists, but is neither a regular file nor a directory\n";
    }
    else
      cout << p << " does not exist\n";
  }

  catch (const filesystem_error& ex)
  {
    cout << ex.what() << '\n';
  }

  return 0;
} 

在 Visual Studio 2010 中为 path fn = it-&gt;path().filename(); 行编译时出现两条错误消息

第一个错误是:'function-style cast' : illegal as right side of '-&gt;' operator,第二个错误是:left of '.filename' must have class/struct/union

此外,当我将鼠标悬停在 path() 上时,它会显示:class boost::filesystem3::path Error: typename not allowed

【问题讨论】:

  • 你能把你找到这段代码的网址贴出来吗?
  • boost 文件系统在某些时候发生了变化。标头将包含#define BOOST_FILESYSTEM_VERSION,其中 boost::filesystem::path::filename() 的返回类型从 boost 版本 2 更改为 3。我有一些带有 #if #else 的代码,在版本 3 中,调用 .string() 使两个版本匹配

标签: c++ boost file-io


【解决方案1】:

此部分(for 的正文)有问题:

  path fn = it->path().filename();   // extract the filename from the path
  v.push_back(fn);                   // push into vector for later sorting
  1. it 指向 path 对象,所以我不明白为什么会调用 path()。好像应该换成it-&gt;filename()
  2. 您将文件名推到同一向量的末尾,因此在循环之后,您将获得包含文件列表的向量两次,首先是路径名,然后是文件名。

编辑:查看原始示例,我发现这些是您的修改。如果您想存储文件名而不是打印它们,请定义另一个 stringpath 向量并将文件名存储在其中,不要重复使用第一个。并且删除对path() 的调用应该可以解决编译问题。

EDIT 2:作为一个可爱的BTW,你可以使用std::transform而不是std::copy一次性实现目录遍历和文件名提取:

struct fnameExtractor {  // functor
  string operator() (path& p) { return p.filename().string(); }
};

vector<string> vs;
vector<path> vp;
transform(directory_iterator(p), directory_iterator(), back_inserter(vs),
          fnameExtractor());

同样使用mem_fun_ref 代替fnameExtractor 函子:

transform(directory_iterator(p), directory_iterator(), back_inserter(vp),
          mem_fun_ref(&path::filename));

【讨论】:

  • 是的,我可能应该提到我修改了它,但这是他们在示例下方的代码:“tut3.cpp 和 tut4.cpp 之间的主要区别在于目录中发生的情况迭代循环。我们将:cout &lt;&lt; " " &lt;&lt; *it &lt;&lt; '\n'; 更改为:path fn = it-&gt;path().filename(); v.push_back(fn);"
  • 用仿函数尝试了你的编辑 2,我收到了这个错误消息:No suitable user-defined conversion from "boost::filesystem3::path" to "std::string" exists.
  • 解决把return p.filename().string();放在仿函数中
  • @ladookie - 对,我忘了。我会编辑。虽然我依稀记得文档说path 定义隐式转换为std::string...
【解决方案2】:

在您发布链接后,发布的代码似乎工作正常。但是在for循环中的修改代码中引入了问题。第一个问题是,path() 是一个构造函数。第二个问题,我不确定boost::path 是否包含filename() 的任何方法。你可以试试,

path fn = (*it);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    相关资源
    最近更新 更多