C++ 如何通过函数指针递归调用类成员函数?

C++ 如何通过函数指针递归调用类成员函数?,c++,recursion,member-function-pointers,C++,Recursion,Member Function Pointers,我正在编写一个库,在树状对象上运行某些算法。我有一个edge_t类,它包含const unsigned int数据成员edge_id和weight,分别作为edge的唯一标识符和edge的权重 我在C++中编写了TreEyt和SudiTeeReTT类,它们都包含映射到EdgEtts的指针。TeeEyt和SudiTeeReTo都是从包含树类对象应该具备的所有功能的抽象Basic TreEyt类派生的,包括以下方法: // returns the sum of the weights of the

我正在编写一个库,在树状对象上运行某些算法。我有一个edge_t类,它包含const unsigned int数据成员edge_id和weight,分别作为edge的唯一标识符和edge的权重

我在C++中编写了TreEyt和SudiTeeReTT类,它们都包含映射到EdgEtts的指针。TeeEyt和SudiTeeReTo都是从包含树类对象应该具备的所有功能的抽象Basic TreEyt类派生的,包括以下方法:

// returns the sum of the weights of the edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::weight(const edge_ptr) const

// returns the number of edge_ts below the edge_t pointed to by edge_ptr
unsigned int basic_tree_t::num_descendents(const edge_ptr) const
我正在编写一些其他代码,用户在其中输入一个tree\u t对象,代码必须从中迭代采样一个子树,进行一些计算,采样另一个子树,进行更多计算,等等。要进行计算,代码需要知道每个子树中每条边的权重和num_子代的值

为了避免重复计算相同的值,每次我构建一个新的子树时,我都会创建std::map weight\u map和std::map num\u substants\u map,它们将子树边的每个边\u id映射到基本树\u t中相应成员函数输出的值,然后处理这些值。我编写了以下函数来填充这些映射:

void populate_weight_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & weight_map)
{
        weight_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.weight(e)));
        for (auto & c : *(e->children))
                if (S.contains(c))
                        populate_weight_map(S, c, weight_map);
}

void populate_num_descendents_map(subtree_t & S, edge_ptr & e, std::map<unsigned int, unsigned int> & num_descendents_map)
{
        num_descendents_map.insert(std::pair<unsigned int, unsigned int>(e->edge_id, S.num_descendents(e)));
        for (auto & c : *(e->children))
                if (S.contains(c))
                        populate_weight_map(S, c, num_descendents_map);
}
但是,编译器在最后一行返回不透明错误:

error: expected unqualified-id
                    populate_map(S, c, m, &basic_tree_t::*f);
                                                         ^
填充映射的第四个参数应该是什么?

f已经是指向所需成员的指针,因此只需传递该参数:

populate_map(S, c, m, f);
&基本树::*f在这种情况下没有意义。它看起来像是试图声明一个指向数据成员的指针,但这并不是您想要的。

f已经是指向所需成员的指针,因此只需传递:

populate_map(S, c, m, f);

&基本树::*f在这种情况下没有意义。这看起来像是试图声明指向数据成员的指针,但无论如何这不是您想要的。

模板可能会用通用std::invoke替换成员函数,std::function可能是替代方法。模板可能会用通用std::invoke替换成员函数,std::function是可能的替代方法。非静态成员函数需要完整限定名:&basic\u tree\u t::f。非静态成员函数需要完整限定名:&basic\u tree\u t::f。