简体   繁体   English

无法编译 C++ 函数

[英]Unable to compile C++ function

My problem might be a petty one, but as a beginner I want to learn the details and hence would be thankful for anyone pointing out my mistakes.我的问题可能很小,但作为初学者,我想了解细节,因此感谢任何指出我错误的人。

In my header file, I have defined a function like this在我的头文件中,我定义了一个这样的函数

void  GetFileNames(const fs::path&, const string&, vector<fs::path>&, const bool);

Then the function definition goes like this in GetFileNames.cpp :然后函数定义在 GetFileNames.cpp 中是这样的:

void GetFileNames(const fs::path& root, const string& ext, vector<fs::path>& names, const bool recursiveflag)
{
        if(!fs::exists(root) || !fs::is_directory(root))
        {
                cout<<"The root path either does not exist, or is not a valid folder.\n"<<endl;
                return;
        }

    if (recursiveflag)
    {
            fs::recursive_directory_iterator it(root);
            fs::recursive_directory_iterator endit;
    }
    else
    {
            fs::directory_iterator it(root);
            fs::directory_iterator endit;
    }

    while(it != endit)
    {
            if(fs::is_regular_file(*it) && it->path().extension() == ext)
                    names.push_back(it->path().filename());

            ++it;
    }
}

When I then try to compile program test.cpp where the function has been called in following way like this :当我然后尝试编译程序 test.cpp 时,该函数已以如下方式调用:

int main(int argc, char** argv)
{
        vector<fs::path> names;
        fs::path root(argv[1]);
        string ext(argv[2]);
        GetFileNames(root,ext,names,true);
        for(auto i = names.begin(); i!= names.end(); ++i)
                cout<< (*i).string()<<endl;
        return 0;
}

I receive the following error :我收到以下错误:

Scanning dependencies of target Test
[ 33%] Building CXX object src/CMakeFiles/Test.dir/test.cpp.o
[ 66%] Building CXX object src/CMakeFiles/Test.dir/GetFileNames.cpp.o
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:22:8: error: use of undeclared identifier 'it'
        while(it != endit)
              ^
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:22:14: error: use of undeclared identifier 'endit'
        while(it != endit)
                    ^
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:24:27: error: use of undeclared identifier 'it'
                if(fs::is_regular_file(*it) && it->path().extension() == ext)
                                        ^
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:24:34: error: use of undeclared identifier 'it'
                if(fs::is_regular_file(*it) && it->path().extension() == ext)
                                               ^
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:25:20: error: use of undeclared identifier 'it'
                        names.push_back(it->path().filename());
                                        ^
/Users/ujjwalujjwal/prog/src/GetFileNames.cpp:27:5: error: use of undeclared identifier 'it'
                ++it;
                  ^
6 errors generated.
make[2]: *** [src/CMakeFiles/Test.dir/GetFileNames.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/Test.dir/all] Error 2
make: *** [all] Error 2

I do not seem to understand that where exactly I am making a conceptual error.我似乎不明白我到底在哪里犯了一个概念错误。 This seems very trivial, but I would like to understand whats going on here.这看起来很微不足道,但我想了解这里发生了什么。

You are defining the iterators within a prior if-else scope:您正在先前的 if-else 范围定义迭代器:

fs::recursive_directory_iterator it(root);
fs::recursive_directory_iterator endit;

Therefore when you reference them later in the while loop they no longer exist as the variables are destroyed as soon as your if-else block is finished.因此,当您稍后在 while 循环中引用它们时,它们不再存在,因为一旦 if-else 块完成,变量就会被销毁。

As you have iterators that are of different types depending on the bool argument, you might have to actually place the while loop within each of your if and else blocks to ensure that the iterators still exist when you execute your while loop.由于您有不同类型的迭代器,具体取决于bool参数,因此您可能必须将 while 循环实际放置在每个ifelse块中,以确保在执行while循环时迭代器仍然存在。

However you choose to proceed it is nothing to do with the boolean parameter to your function - pay attention to the compiler errors as they are telling you what is wrong.无论您选择继续,它都与函数的boolean参数无关 - 请注意编译器错误,因为它们会告诉您什么是错误的。

it是在另一个块中定义的,你不能在你的 while 循环中使用它,除非你之前定义它

The problem is this bit of code...问题是这段代码......

    ....
    while(it != endit)
    {
        if(fs::is_regular_file(*it) && it->path().extension() == ext)
                names.push_back(it->path().filename());

        ++it;
    }
    ....

...is using two variables, 'it' and 'endit'. ...正在使用两个变量,'it' 和 'endit'。 However in both cases where you declare these are inside {} blocks.但是,在您声明这些都在 {} 块内的两种情况下。 This makes the declaration only local to these blocks.这使得声明仅适用于这些块。

Your problem is made slightly worse by each declaration of 'it' and 'endit' being of different types. 'it' 和 'endit' 的每个声明都是不同类型的,这会使您的问题变得更糟。

You can either move the declaration outside of the local blocks and make them of some common type (in this case InputIterators) or you can move the while loop at the end inside both local blocks like this...您可以将声明移动到本地块之外并使它们具有某种常见类型(在本例中为 InputIterators),或者您可以将 while 循环移动到两个本地块内的末尾,如下所示...

...
    if (recursiveflag)
    {
        fs::recursive_directory_iterator it(root);
        fs::recursive_directory_iterator endit;

        while(it != endit)
        {
            if(fs::is_regular_file(*it) && it->path().extension() == ext)
                    names.push_back(it->path().filename());

            ++it;
        }
    }
    else
    {
        fs::directory_iterator it(root);
        fs::directory_iterator endit;

        while(it != endit)
        {
            if(fs::is_regular_file(*it) && it->path().extension() == ext)
                    names.push_back(it->path().filename());

            ++it;
        }
    }
...

Although this leads to unnecessary code duplication and makes for maintenance nightmares further on.尽管这会导致不必要的代码重复,并进一步导致维护噩梦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM