[英]Help me debug this - 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( "." ) ;
}
Wen我正在嘗試運行此程序,但出現錯誤
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’
坦克斯提前。 PS此程序將列出所有文件/文件夾
由於我不能簡單地在當前答案中添加評論,所以我想記一下
不推薦使用boost :: filesystem :: wpath :: native_file_string()並更改為boost :: filesystem :: wpath :: string()。 所以下面一行
cout << iter->native_file_string() << " (file)\n" ;
變成
cout << iter->string() << " (file)\n" ;
為了使此功能正常運行,您需要進行兩項更改。 首先,迭代器返回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( "." ) ;
}
我快速瀏覽了一下文檔,但找不到相對native_directory_string
或native_file_string
。 AFAICT,這些成員函數屬於另一個類( filesystem::path
),我認為您可以從basic_directory_entry
訪問該類:
iter->path().native_directory_string()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.