【问题标题】:Problems compiling C++ code with boost library使用 boost 库编译 C++ 代码时出现问题
【发布时间】:2013-06-17 13:51:48
【问题描述】:

我正在尝试编译 (thomas pevny's source code to calculate the subtractive pixels adjacency matrix)。此代码要求预先安装 libboost 和 libpng 库,我已成功完成。

但是当我执行“make”命令时,终端上会出现以下错误。

spam.cpp:169:26: error: ‘class boost::filesystem3::directory_entry’ has no member named ‘leaf’
spam.cpp:179:20: error: ‘class boost::filesystem3::path’ has no member named ‘native_file_string

有没有办法解决这个问题?我应该安装另一个 libboost 版本吗?

感谢您的关注。

【问题讨论】:

    标签: c++ boost makefile


    【解决方案1】:

    leaf() 已弃用。

    查看已弃用的函数列表及其新名称:

    http://www.boost.org/doc/libs/1_45_0/libs/filesystem/v2/doc/index.htm

    编辑评论:

    应该是这样的:

      boost::filesystem::path p("foo.txt");
      std::cout << p.filename() << std::endl; 
    

    【讨论】:

    • 我通过将 native_file_string 替换为 file_string() 更正了第二个错误,但是当我将 leaf() 替换为 path().filename() 时,又出现了另一个错误。这段代码是:string fileName=file->path().filename(); //之前是file->leaf()。
    • 错误是这样的: spam.cpp:168:42: error: 'boost::filesystem3::path' 到非标量类型'std::string {aka std:: 的转换basic_string}' 必需
    • 再次感谢萨尔加。但我希望代码独立于文件名运行(我不想直接在代码上通知文件名)。有没有什么好方法可以改变这段代码?再次感谢您。
    • 这段代码是string fileName=file->path().filename()。我应该将 file->path().filename() 转换为字符串吗?
    【解决方案2】:

    leaf() 已弃用。见:http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/reference.html#directory_iterator-members 您可以尝试不使用BOOST_FILESYSTEM_NO_DEPRECATED

    【讨论】:

    • 感谢您的回复。我通过将 native_file_string 替换为 file_string() 来纠正第二个错误,但是当我将 leaf() 替换为 path().filename() 时,又出现了另一个错误。这段代码是:string fileName=file->path().filename(); //之前是file->leaf()。变量文件是一个目录迭代器。错误是 错误是这样的: spam.cpp:168:42: error: conversion of 'boost::filesystem3::path' to non scalar type 'std::string {aka std::basic_string} ' 必填
    • path.filename() 不是String 类型,而是path 类型。如果您想从中获取String,可以尝试native() (path.filename().native())。
    • 再次感谢您的回复。现在我有以下错误: spam.cpp:168:30: error:'((boost::iterator_facade<:filesystem3::directory_iterator boost::filesystem3::directory_entry boost::single_pass_traversal_tag>*)(&文件))->boost::iterator_facade::operator-><:filesystem3::directory_iterator boost::filesystem3::directory_entry boost::single_pass_traversal_tag boost:: filesystem3::directory_entry long int>()->boost::filesystem3::directory_entry::path' 没有类类型。我会继续努力修复它,再次感谢您。
    【解决方案3】:

    通过@Salgar 和@Jean-Baptiste Yunès 的建议以及在makefile 中的-lboost_filesystem 之后添加-lboost_system 解决了这个问题。谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多