c++ - 帮我调试这个 - C++ Boost

标签 c++ boost

我是 C++ Boost 的菜鸟。任何人都可以帮我调试这个程序。

#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem; 
using namespace std;

void show_files( const path & directory, bool recurse_into_subdirs = true )
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
      if ( is_directory( *iter ) )
      {
        cout << iter->native_directory_string() << " (directory)\n" ;
        if( recurse_into_subdirs ) show_files(*iter) ;
      }
      else 
        cout << iter->native_file_string() << " (file)\n" ;
  }
}

int main()
{
    show_files( "." ) ;
}

当我尝试运行这个程序时,我遇到了类似

的错误
ex2.cpp: In function ‘void show_files(const boost::filesystem2::path&, bool)’:
ex2.cpp:15: error: ‘class boost::filesystem2::basic_directory_entry<boost::filesystem2::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem2::path_traits> >’ has no member named ‘native_directory_string’
ex2.cpp:19: error: ‘class boost::filesystem2::basic_directory_entry<boost::filesystem2::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem2::path_traits> >’ has no member named ‘native_file_string’

提前 Tanx。附言该程序将列出所有文件/文件夹

最佳答案

要使其正常工作,您需要进行两项更改。首先,迭代器返回 basic_directory_entry 的实例,而不是路径。所以首先你需要从迭代器中查询路径。此外,较新版本的 boost 已经从访问器方法中删除了 native_ 前缀。

这是您的代码,其中包含更改:

#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem; 
using namespace std;

void show_files( const path & directory, bool recurse_into_subdirs = true )
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
    if ( is_directory( *iter ) )
    {
      cout << iter->path().directory_string() << " (directory)\n" ;
      if( recurse_into_subdirs ) show_files(*iter) ;
    }
    else 
      cout << iter->path().file_string() << " (file)\n" ;
  }
}

int main()
{
    show_files( "." ) ;
}

关于c++ - 帮我调试这个 - C++ Boost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4407500/

相关文章:

c++ - 在编译之前解析文件

c++ - GetProcAddress 与所有加载的库

c++ - 如何在 Visual Studio Code 中构建和运行 C++ 代码?

c++ - 如何使用mongodb的c++驱动程序构建程序?

c++ - boost 日期时间 : time_duration from seconds to multiple days

c++ - 几何、交集

android - Android 是否抽象了设备架构?

c++ - Mountain Lion 上的 Mex 文件 : explicit instantiation error

c++ - ASIO 库 - 未调用处理程序

c++ - Boost.Asio iostream 冲洗不工作?