Помогите мне отладить это - C ++ Boost - PullRequest
1 голос
/ 10 декабря 2010

Я новичок в 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’

Тэнкс заранее. Постскриптум Эта программа перечислит все файлы / папки

Ответы [ 3 ]

4 голосов
/ 10 декабря 2010

Вам нужно внести два изменения, чтобы это работало правильно.Прежде всего, итератор возвращает экземпляр basic_directory_entry, а не путь.Итак, сначала вам нужно запросить path у итератора.Кроме того, в новых версиях 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( "." ) ;
}
4 голосов
/ 04 апреля 2013

Поскольку я не могу просто добавить комментарий к (на данный момент) верхнему ответу, я хотел бы отметить, что

boost :: filesystem :: wpath :: native_file_string () устарела и заменена наповышение :: файловой системы :: WPATH :: строки ().Итак, следующая строка

cout << iter->native_file_string() << " (file)\n" ;

становится

cout << iter->string() << " (file)\n" ;
1 голос
/ 10 декабря 2010

Я быстро взглянул на документацию и не могу найти упоминаний о native_directory_string или native_file_string против basic_directory_entry. AFAICT, эти функции-члены принадлежат другому классу (filesystem::path), к которому, я думаю, вы можете получить доступ из basic_directory_entry, таким образом:

iter->path().native_directory_string()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...