I’m using a vector of pointers to free up a series of node objects in the heap. The vector has all the node object addresses and there is a function, delete_nodes, which is used with the for_each loop to delete all nodes in the vector. For some reason I get the following error in eclipse cdt with the for_each loop underlined in red:
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>)'
The code is for Huffman coding, and the for_each loop is at the very end. The nodes_delete vector is created right before the while loop.
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;
}
It look like your
delete_nodesis a non-static member function. If so, you cannot just usedelete_nodesas an argument forstd::for_each.std::for_eachrequires a functor. Yourdelete_nodesis not a functor.Firstly, to obtain a pointer to a non-static member function,
&operator and a qualified name are always required. A mere name of non-static member function (justdelete_nodes) is not a valid expression in C++. You have to do&Huff::delete_nodes.Secondly, again, a pointer to a member function (as opposed to a pointer to an “ordinary” function) is not a functor. In order to turn it into a functor you can use
std::mem_funfunction. That will give you a binary functor, sincestd::mem_funwill turn the implicitthisparameter into an explicit one. In order to turn it into a unary functor required bystd::for_eachyou have to bind the first argument to a specific object pointer value (thisprobably?).The end result of the above steps will look as
This is a unary functor that calls
delete_nodesforthisobject.So, the
for_eachcall in your example should look as followsHowever, it looks like in your implementation
delete_nodescan be turned into a static member function. A static member function is an “ordinary” function, meaning that it is a functor and it can be used directly. I.e. if you makedelete_nodesstatic your code should work as is.Decide what path you wish to follow and make the necessary changes.