0

ファイルシステム上のパスへの参照を取り込み、ファイル名をベクトルに再帰的に追加する関数を作成しています。しかし、まずベクトルにパスを追加できるようにする必要があります。

この方法の何が問題なのですか?

namespace filesys = boost::filesystem;

方法:

void recursive_file_list(filesys::path & root_path, vector<filesys::path> & paths) {
    paths->push_back(*root_path);    // line 14

    // TODO: add more logic to actually recurse through file system
    // for now just add root_path (even though root_path is path of directory, not file)
}

そして、私はそれを次のように呼びます:

    int main(int argc, char* argv[]) {

        // Use the command line arguments
        filesys::path abs_path;    // line 23
        if ( argc > 1 )
            // Make the system complete this path to absolute path
            abs_path = filesys::system_complete( filesys::path( argv[1] ) );
        else {
            // If no arguments given
            cout << "usage:   list_files [path]" << endl;
            exit(1);
        }

        // Is this a valid path?
        if (!filesys::exists( abs_path )) {
            cout << "The path you have specified does not exist." << endl; 
            exit(2);
        }

        // If this is a directory
        vector<filesys::path> filepaths();   
        if (filesys::is_directory( abs_path )) {
            cout << "You have specified a directory." << endl;
            recursive_file_list(&abs_path, &filepaths);
        }

        else {
            cout << "You have specified a file." << endl;
        }
    }

エラー:

list_files.cpp: In function 'void recursive_file_list(boost::filesystem3::path&, std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >&)':
list_files.cpp:14: error: base operand of '->' has non-pointer type 'std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >'
list_files.cpp:14: error: no match for 'operator*' in '*root_path'
list_files.cpp: In function 'int main(int, char**)':
list_files.cpp:43: error: invalid initialization of non-const reference of type 'boost::filesystem3::path&' from a temporary of type 'boost::filesystem3::path*'
list_files.cpp:13: error: in passing argument 1 of 'void recursive_file_list(boost::filesystem3::path&, std::vector<boost::filesystem3::path, std::allocator<boost::filesystem3::path> >&)'

わかりません-参照で渡し、逆参照して追加しています...何が間違っているのかわかりません。

4

1 に答える 1

4

参照を逆参照することはできません! *and演算子は->参照には適用されません。path自分自身を自分自身にプッシュしvectorます:

paths.push_back(root_path);    // line 14

さらに、参照引数にアドレスを渡さず、引数自体のみを渡すため、次のように関数を呼び出します。

recursive_file_list(abs_path, filepaths);

参照は、ポインタのような非コピー セマンティクスをすべて独自に持っています。アドレスを取得したり、逆参照しようとしたりして、それらを支援する必要はありません。

于 2012-06-03T02:07:13.030 に答える