Am, with baby steps, becoming acquainted with the boost library. So far I'm compiling examples.

From the tutorial, there is a very short regex-example, mostly there to test that the library is linked correctly:

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

and this compiles (and runs) like a charm:

g++ boostTest.cpp -o boostTest -lboost_regex

However, going into the tutorial on the filesystem library, there is another example, simple_ls.cpp:

//  simple_ls program  -------------------------------------------------------//

//  © Copyright Jeff Garland and Beman Dawes, 2002

//  Use, modification, and distribution is subject to the Boost Software
//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/filesystem for documentation.

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <iostream>

namespace fs = boost::filesystem;

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

  fs::path full_path( fs::initial_path() );

  if ( argc > 1 )
    full_path = fs::system_complete( fs::path( argv[1], fs::native ) );
  else
    std::cout << "\nusage:   simple_ls [path]" << std::endl;

  unsigned long file_count = 0;
  unsigned long dir_count = 0;
  unsigned long err_count = 0;

  if ( !fs::exists( full_path ) )
  {
    std::cout << "\nNot found: " << full_path.native_file_string() << std::endl;
    return 1;
  }

  if ( fs::is_directory( full_path ) )
  {
    std::cout << "\nIn directory: "
              << full_path.native_directory_string() << "\n\n";
    fs::directory_iterator end_iter;
    for ( fs::directory_iterator dir_itr( full_path );
          dir_itr != end_iter;
          ++dir_itr )
    {
      try
      {
        if ( fs::is_directory( *dir_itr ) )
        {
          ++dir_count;
          std::cout << dir_itr->leaf()<< " [directory]\n";
        }
        else
        {
          ++file_count;
          std::cout << dir_itr->leaf() << "\n";
        }
      }
      catch ( const std::exception & ex )
      {
        ++err_count;
        std::cout << dir_itr->leaf() << " " << ex.what() << std::endl;
      }
    }
    std::cout << "\n" << file_count << " files\n"
              << dir_count << " directories\n"
              << err_count << " errors\n";
  }
  else // must be a file
  {
    std::cout << "\nFound: " << full_path.native_file_string() << "\n";    
  }
  return 0;
}

which I can not compile at all (the file is now called boostTest.cpp):

g++ boostTest.cpp -o boostTest -lboost_filesystem
boostTest.cpp: In function ‘int main(int, char**)’:
boostTest.cpp:23:68: error: invalid conversion from ‘bool (*)(const string&) {aka bool (*)(const std::basic_string<char>&)}’ to ‘boost::enable_if_c<true, void>::type* {aka void*}’ [-fpermissive]
/usr/include/boost/filesystem/v3/path.hpp:130:5: error:   initializing argument 2 of ‘boost::filesystem3::path::path(const Source&, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type> >::type*) [with Source = char*, typename boost::enable_if<boost::filesystem3::path_traits::is_pathable<typename boost::decay<Source>::type> >::type = void]’ [-fpermissive]
boostTest.cpp:33:47: error: ‘class boost::filesystem3::path’ has no member named ‘native_file_string’
boostTest.cpp:40:28: error: ‘class boost::filesystem3::path’ has no member named ‘native_directory_string’
boostTest.cpp:51:33: error: ‘class boost::filesystem3::directory_entry’ has no member named ‘leaf’
boostTest.cpp:56:33: error: ‘class boost::filesystem3::directory_entry’ has no member named ‘leaf’
boostTest.cpp:62:31: error: ‘class boost::filesystem3::directory_entry’ has no member named ‘leaf’
boostTest.cpp:71:43: error: ‘class boost::filesystem3::path’ has no member named ‘native_file_string’

I have made the small change in simple_ls.cpp to enclose the boost-headers in < > instead of " ", as that works fine for the regex-example.

Please note that both libraries requires linking to binaries, so that's not the main difference. I am assuming that my compiler call is wrong somehow (as you would expect a tutorial example to be compileable), but I really don't see very many places in what I've done to insert an error.

I have checked, and the headers are indeed where they're supposed to be.

I hope someone can spot the error :)

Recommended Answers

All 2 Replies

Didn't you get a makefile with those examples?
Try looking for one and compiling with it, that should work.

Also, looking at the file header, it is an example from 2002, boost might have changed since that time.

Indeed you are right, the example is outdated. Making my way down to the bottom of the page, it turns out that google has pointed me to the documentation for a quite old version.
Bad google.

So, I guess it WAS an incompileable tutorial example. Finding a small example at the correct website (also among the google results, close by), it compiles and runs, so it seems I was doing it right.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.