0

I know there are already several topics related to overloading the copy and assignment operators of base classes - but after two days of trying, I wasn't able to properly do this using boost::filesystem::path.

What I currently have:

using namespace boost::filesystem;
class MyPath: public path
{
  public:

    template<class C>
    MyPath(const C* p): path(p)// dispatch to base copy constructor
    {
      //http://stackoverflow.com/questions/1226634/how-to-use-base-classs-constructors-and-assignment-operator-in-c
      //this one seems to be OK
    }

    MyPath& operator=(const MyPath& p)
    {
      path::operator=(p);
      return *this;
    }

    bool myStuff(){/*...*/}
    /* ... */
}

Then, I use the MyPath class just like 'path' before:

using namespace std;
using namespace boost::filesystem;

list<MyPath> files_list;
MyPath input_folder("a valid path");

copy(recursive_directory_iterator(input_folder), recursive_directory_iterator(), back_inserter(files_list));

This code works OK with path, but with MyPath I have problems with the assignment operator, and I haven't figured out how yet what is the format of the operator= that it's required by the std::list and the iterators..

g++ -g -Ic:/mingw/include -c main.cpp
In file included from c:\mingw\include\c++\4.8.1\bits\char_traits.h:39:0,
                 from c:\mingw\include\c++\4.8.1\ios:40,
                 from c:\mingw\include\c++\4.8.1\ostream:38,
                 from c:\mingw\include\c++\4.8.1\iostream:39,
                 from main.cpp:1:
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h: In instantiation of 'static _OI std::__copy_move<<anonymous>, <anonymous>, <template-parameter-1-3> >::__copy_m(_II, _II, _OI) [with _II =     boost::filesystem::recursive_directory_iterator; _OI = std::back_insert_iterator<std::list<MyPath> >; bool <anonymous> = false; bool <anonymous> = false; <template-parameter-1-3> = std::input_iterator_tag]':
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:390:70:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = boost::filesystem::recursive_directory_iterator; _OI =     std::back_insert_iterator<std::list<MyPath> >]'
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:428:38:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = boost::filesystem::recursive_directory_iterator; _OI =     std::back_insert_iterator<std::list<MyPath> >]'
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:460:17:   required from '_OI std::copy(_II, _II, _OI) [with _II = boost::filesystem::recursive_directory_iterator; _OI = std::back_insert_iterator<std::list<    MyPath> >]'
main.cpp:23:115:   required from here
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:305:16: error: no match for 'operator=' (operand types are 'std::back_insert_iterator<std::list<MyPath> >' and 'boost::filesystem::directory_entry')
      *__result = *__first;
                ^
c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:305:16: note: candidates are:
In file included from c:\mingw\include\c++\4.8.1\bits\stl_algobase.h:67:0,
                 from c:\mingw\include\c++\4.8.1\bits\char_traits.h:39,
                 from c:\mingw\include\c++\4.8.1\ios:40,
                 from c:\mingw\include\c++\4.8.1\ostream:38,
                 from c:\mingw\include\c++\4.8.1\iostream:39,
                 from main.cpp:1:
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:436:7: note: std::back_insert_iterator<_Container>& std::back_insert_iterator<_Container>::operator=(const typename _Container::value_type&) [with _Container =     std::list<MyPath>; typename _Container::value_type = MyPath]
       operator=(const typename _Container::value_type& __value)
       ^
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:436:7: note:   no known conversion for argument 1 from 'boost::filesystem::directory_entry' to 'const value_type& {aka const MyPath&}'
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:443:7: note: std::back_insert_iterator<_Container>& std::back_insert_iterator<_Container>::operator=(typename _Container::value_type&&) [with _Container =     std::list<MyPath>; typename _Container::value_type = MyPath]
       operator=(typename _Container::value_type&& __value)
       ^
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:443:7: note:   no known conversion for argument 1 from 'boost::filesystem::directory_entry' to 'std::list<MyPath>::value_type&& {aka MyPath&&}'
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:402:11: note: std::back_insert_iterator<std::list<MyPath> >& std::back_insert_iterator<std::list<MyPath> >::operator=(const std::back_insert_iterator<std::list<    MyPath> >&)
     class back_insert_iterator
           ^
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:402:11: note:   no known conversion for argument 1 from 'boost::filesystem::directory_entry' to 'const std::back_insert_iterator<std::list<MyPath> >&'
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:402:11: note: std::back_insert_iterator<std::list<MyPath> >& std::back_insert_iterator<std::list<MyPath> >::operator=(std::back_insert_iterator<std::list<MyPath>     >&&)
c:\mingw\include\c++\4.8.1\bits\stl_iterator.h:402:11: note:   no known conversion for argument 1 from 'boost::filesystem::directory_entry' to 'std::back_insert_iterator<std::list<MyPath> >&&'
make: *** [main.o] Error 1

1 Answers1

0

The compiler is telling you what you need:

'operator=' (operand types are 'std::back_insert_iterator<std::list<MyPath> >' and 'boost::filesystem::directory_entry')

So you need to implement one more method (at least).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Yes, I saw that - but something like "std::back_insert_iterator>& operator=(const boost::filesystem::directory_entry& p)" doesn't work. I am not sure what is the expected prototype of the operator= function in this case; I thought it should only be something like MyPath& operator=(const MyPath& p). I am not sure why the compiler expects it to return a std::back_insert_iterator> – Paranoid Sep 14 '13 at 20:16
  • Try with a regular "for" loop instead of the STL `copy()`. – John Zwinck Sep 15 '13 at 06:16