扩展boost :: filesystem :: path并重载赋值运算符

时间:2013-09-14 09:25:21

标签: c++ boost operator-overloading

我知道有几个主题与重载基类的复制和赋值运算符有关 - 但经过两天的尝试,我无法使用boost :: filesystem :: path正确地执行此操作。 / p>

我现在有什么:

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(){/*...*/}
    /* ... */
}

然后,我使用MyPath类就像&#39; path&#39;之前:

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

这段代码可以正常使用路径,但是使用MyPath我遇到了赋值运算符的问题,而且我还没弄清楚运算符的格式是什么=它是std所要求的:: list和迭代器..

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 个答案:

答案 0 :(得分:0)

编译器告诉你你需要什么:

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

所以你需要再实现一个方法(至少)。