我知道已经有几个主题与重载基类的复制和赋值运算符有关 - 但经过两天的尝试,我无法使用 boost::filesystem::path 正确地做到这一点。
我目前拥有的:
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 类:
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 和迭代器所需的 operator= 的格式是什么。
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