user3489620 user3489620 - 1 month ago 12
C++ Question

Boost directory_iterator: 'begin' and 'end' not declared

I am trying to learn how to use the Boost C++ library, but I've run into a problem. When running the following boost::filesystem tutorial code:

#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}

path p (argv[1]);

try
{
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';

else if (is_directory(p))
{
cout << p << " is a directory containing:\n";

for (directory_entry& x : directory_iterator(p))
cout << " " << x.path() << '\n';
}
else
cout << p << " exists, but is not a regular file or directory\n";
}
else
cout << p << " does not exist\n";
}

catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}

return 0;
}


and compiling with:

g++ -std=c++11 main.cpp -o main -L$BOOST_ROOT -lboost_filesystem -lboost_system


or:

g++ -std=c++11 main.cpp -o main -L$BOOST_ROOT -lboost_system -lboost_filesystem


where BOOST_ROOT is the path where I extracted boost_1_62_0.tar.gz, I get the following error:

FoamData.cpp:36:76: error: ‘begin’ was not declared in this scope
for (auto&& x : boost::filesystem::directory_iterator(p))
^
FoamData.cpp:36:76: note: suggested alternative:
In file included from /usr/include/c++/5/vector:66:0,
from FoamData.hpp:4,
from FoamData.cpp:1:
/usr/include/c++/5/bits/range_access.h:87:5: note: ‘std::begin’
begin(_Tp (&__arr)[_Nm])
^
FoamData.cpp:36:76: error: ‘end’ was not declared in this scope
for (auto&& x : boost::filesystem::directory_iterator(p))
^
FoamData.cpp:36:76: note: suggested alternative:
In file included from /usr/include/c++/5/vector:66:0,
from FoamData.hpp:4,
from FoamData.cpp:1:
/usr/include/c++/5/bits/range_access.h:97:5: note: ‘std::end’
end(_Tp (&__arr)[_Nm])
^
make: *** [FoamData.o] Error 1


I apologize, in advance, if I have glossed over something obvious.

EDIT

The following statement from subsequent boost_filesystem tutorials gave the same error:

for(auto&& x : directory_iterator(p))


Code source:
boost::filesystem tutorials

Answer

Try using the following syntax instead:

   if (is_directory(p))
   {
      directory_iterator end_iter;

      for (directory_iterator dir_itr(p);
          dir_itr != end_iter;
          ++dir_itr)
      {
         std::cout << dir_itr->path().filename() << "\n";
      }
   }

This will list all the files in the directory.

The problem with your code is that you're not treating the object returned by directory_iterator(p) as an iterator.

The syntax you are using for the for loop expects a range expression, which should in this case be an object that has a begin and end method for getting at iterators. As you can see from the errors, the compiler is looking for these members.

Howerver, you've already created the begin iterator explictly - you're 'too late' to use the range syntax for the for loop.