0

我正在使用指针向量来释放堆中的一系列节点对象。该向量具有所有节点对象地址,并且有一个函数 delete_nodes,它与 for_each 循环一起用于删除向量中的所有节点。出于某种原因,我在 eclipse cdt 中收到以下错误,其中 for_each 循环用红色下划线:

error: no matching function for call to 'for_each(__gnu_cxx::__normal_iterator<Node**, std::vector<Node*, std::allocator<Node*> > >, __gnu_cxx::__normal_iterator<Node**, std::vector<Node*, std::allocator<Node*> > >, <unresolved overloaded function type>)'

该代码用于 Huffman 编码,for_each 循环位于最后。nodes_delete 向量在 while 循环之前创建。

void Huff::delete_nodes(Node*n){//this is used to delete all the nodes in the binary tree at the end of Huff::compress()
    delete n;
}
vector<Code>* Huff::compress(){
    //-------GETTING WEIGHTS/FREQUENCIES------
    vector<Node *>* nodes = new vector<Node*>; // Vector of nodes for later use
    map<char, int>* freq = new map<char, int>; //  Map to find weight of nodes
    for(unsigned int i = 0; i < content.length(); i++)
        (*freq)[content[i]]++; 
    CopyTo copyto(nodes); //sets vector<Node*> to copy to 
    for_each(freq->begin(), freq->end(), copyto); // Copies 
    delete freq;
    vector<Node *>::iterator beg = nodes->begin();

    //-------SETTING UP TO BUILD TREE------
    if(nodes->size() % 2 == 1){ //makes sure there are an even number of nodes
        Node* fill = new Node;
        fill->set_node(0, '*', NULL, NULL);
        nodes->push_back(fill);
    }
    huff_sort(nodes); // sort nodes by weight
    vector<Node*> nodes_delete(*nodes); //this is used to delete all the nodes in the binary tree at the end
    //-------BUILDING TREE------
    while(nodes->size() != 1){ //Sorts nodes by weight and then removes two of them and replaces them with one
        int w= (**beg).weight + (**(beg+1)).weight;
        Node* p = new Node;
        p->set_node(w, '*', *nodes->begin(), *(nodes->begin()+1)); //making it the parent node of the two lowest nodes
        nodes->erase(nodes->begin(), nodes->begin()+2);
        unsigned int i = 0;
        while(w > (*nodes)[i]->weight && i <= nodes->size()){ //finds where to insert the parent node based on weight
            i++;
        }
        if(i > nodes->size()) //if it needs to be inserted at the end
            nodes->push_back(p);
        else
            nodes->insert(nodes->begin()+i, p);
    }
    //-------TRAVERSING TREE------
    Node* root = (*nodes)[0];
    delete nodes;
    vector<Code>* codes = new vector<Code>;
    traverse(root, codes , "");
    delete root;
    for_each(nodes_delete.begin(), nodes_delete.end(), delete_nodes);
    return codes;
}
4

2 回答 2

3

您正在尝试将无界成员函数作为仿函数传递。您必须使用例如std::mem_fnbind将其绑定到当前对象。

于 2012-07-25T02:00:09.557 回答
3

看起来你delete_nodes是一个非静态成员函数。如果是这样,您不能仅将delete_nodes其用作std::for_each. std::for_each需要一个函子。你delete_nodes不是函子。

首先,要获得指向非静态成员函数的指针,&始终需要运算符和限定名称。非静态成员函数的名称(just delete_nodes)在 C++ 中不是有效的表达式。你必须做&Huff::delete_nodes

其次,指向成员函数的指针(与指向“普通”函数的指针相反)不是函子。为了把它变成一个仿函数,你可以使用std::mem_fun函数。这将为您提供一个二元仿函数,因为std::mem_fun会将隐式this参数转换为显式参数。为了将其转换为所需的一元仿函数,std::for_each您必须将第一个参数绑定到特定的对象指针值(this可能?)。

上述步骤的最终结果将如下所示

bind1st(mem_fun(&Huff::delete_nodes), this)

这是一个调用对象delete_nodes的一元函子。this

因此,for_each您示例中的调用应如下所示

for_each(nodes_delete.begin(), nodes_delete.end(),
  bind1st(mem_fun(&Huff::delete_nodes), this));

但是,看起来在您的实现delete_nodes中可以变成静态成员函数。静态成员函数是一个“普通”函数,这意味着它是一个函子,可以直接使用。即,如果您将delete_nodes其设为静态,则您的代码应该按原样工作。

确定您希望遵循的路径并进行必要的更改。

于 2012-07-25T02:02:46.593 回答