For the template class declarations:
enum class traversal_order {PREORDER, INORDER, POSTORDER};
template<typename T1>
class node{
//public and private members
};
template<typename T1, typename T2>
class tree{
//rest of template
public:
using sp2node_cont = vector<shared_ptr<node<T1>>>;
using r_iter_sp2node_cont = typename vector<shared_ptr<node<T1>>>::iterator&;
public:
tree(T2 const&, size_t);
void traversal_tree(traversal_order);
//rest of template
private:
sp2node_cont impl_tree;
};
And for the following definition of the template method:
template<typename T1, typename T2>
tree<T1,T2>::traverse_tree(traversal_order D){
switch(D){
case PREORDER:
{
function<void(r_iter_sp2node_cont,r_iter_sp2node_cont)>
print_preorder = [&] (r_iter_sp2node_cont begin,
r_iter_sp2node_cont end
){
//rest of the definition
print_preorder(std::next(begin), end);
};
print_preoder(impl_tree.begin(), impl_tree.end());
break;
}
}
}
The compiler generates the following error for the recursive invocation of the std::function print_preorder()
when invoked from main(), after constructing the tree object as follows:
std::array<string,25> A;
//rest of the code
tree<string, decltype(A)> a_tree(A,A.size());
a_tree.traverse_tree(traversal_order::PREORDER);
1 In file included from main.cpp:1:
2 ./bt.hpp:306:21: error: no matching function for call to object of type 'function<void (r_iter_sp2node_cont, r_iter_sp2node_cont)>' (aka 'function<void (__wrap_iter<std::__1::shared_ptr<node<std::__1:: basic_string<char> > > *> &, __wrap_iter<std::__1::shared_ptr<node<std:: __1::basic_string<char> > > *> &)>')
3 print_preorder(next(begin), end);
4 ^~~~~~~~~~~~~~
5 main.cpp:23:12: note: in instantiation of member function 'tree<std::__1:: basic_string<char>, std::__1::array<std::__1::basic_string<char>, 25> >:: traverse_tree' requested here
6 a_tree.traverse_tree(traversal_order::PREORDER);
7 ^
8 /Library/Developer/CommandLineTools/usr/include/c++/v1/functional:1667:9: note: candidate function not viable: expects an l-value for 1st argument
9 _Rp operator()(_ArgTypes...) const;
10 ^
11 1 error generated.
I am not sure how to resolve this error. Any suggestions?
Your issue is in this line
using r_iter_sp2node_cont = typename vector<shared_ptr<node<T1>>>::iterator&;
you have defined r_iter_sp2node_cont
as reference to iterator.
So the signature of print_preorder
is
print_preorder(vector...::iterator&, vector...::iterator&)
But when you call
print_preorder(impl_tree.begin(), impl_tree.end());
methods of vector begin()
and end()
returns iterator
objects. You cannot pass temporary iterator
object returned by these member functions to function which expects reference to iterator iterator&
.
You should remove &
from definition r_iter_sp2node_cont
type.
PS. Your code can be compiled under MSVC which has extension and temporary object can be passed to function with expects reference to object void foo(anObject&)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.