c++ pushing dereferenced objects to vector

时间:2021-12-05 21:17:47

I'm making a function to take in a reference to a path on the filesystem and recursively add file names to a vector. But first I have to be able to add paths to a vector.

我正在创建一个函数来接受对文件系统上的路径的引用,并递归地将文件名添加到向量中。但首先我必须能够向矢量添加路径。

what is wrong with this method?

这个方法有什么问题?

namespace filesys = boost::filesystem;

Method:

方法:

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)
}

And I call it like so:

我称之为:

    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;
        }
    }

Error:

错误:

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> >&)'

I don't understand - I'm passing it by reference and then dereferencing it to add it...not sure what I'm doing wrong.

我不明白 - 我通过引用传递它然后解除引用它来添加它...不确定我做错了什么。

1 个解决方案

#1


4  

You can't dereference references! The * and -> operators do not apply to references; you would push the path itself to the vector itself:

你不能取消引用参考! *和 - >运算符不适用于引用;你会把路径本身推到矢量本身:

paths.push_back(root_path);    // line 14

and furthermore, you don't pass addresses to reference arguments, just the arguments themselves, so you would call the function like this:

此外,您不会将地址传递给引用参数,只传递参数本身,因此您可以像这样调用函数:

recursive_file_list(abs_path, filepaths);

References have pointer-like non-copy semantics all by themselves; you do not need to help them along by taking addresses or trying to dereference them.

引用本身具有类似指针的非复制语义;您不需要通过获取地址或尝试取消引用它们来帮助它们。

#1


4  

You can't dereference references! The * and -> operators do not apply to references; you would push the path itself to the vector itself:

你不能取消引用参考! *和 - >运算符不适用于引用;你会把路径本身推到矢量本身:

paths.push_back(root_path);    // line 14

and furthermore, you don't pass addresses to reference arguments, just the arguments themselves, so you would call the function like this:

此外,您不会将地址传递给引用参数,只传递参数本身,因此您可以像这样调用函数:

recursive_file_list(abs_path, filepaths);

References have pointer-like non-copy semantics all by themselves; you do not need to help them along by taking addresses or trying to dereference them.

引用本身具有类似指针的非复制语义;您不需要通过获取地址或尝试取消引用它们来帮助它们。