我正在使用指针向量来释放堆中的一系列节点对象。该矢量具有所有节点对象地址,并且有一个函数delete_nodes,该函数与for_each循环一起用于删除矢量中的所有节点。出于某种原因,我得到了Eclipse CDT的用的for_each循环以下错误红色下划线: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>)'
代码为霍夫曼编码和循环的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;
}
“delete_nodes”函数是否有多个(重载)定义?在上面的代码中,我只能看到一个,但是您是否检查过是否存在另一个,也许在一个头文件中? – jogojapan 2012-07-25 01:58:48
@jogojapan就我所知,没有其他的delete_nodes。另外,如果我将delete_nodes的名称更改为其他任何名称,则错误仍然存在。 – 2012-07-25 02:02:00
顺便说一句我假设'delete_nodes'函数被定义为一个静态成员函数。这实际上是正确的吗?如果不是的话,下面的Matteo Italia是正确的(尽管我发现编译器的错误消息在这种情况下会引起误导)。 – jogojapan 2012-07-25 02:02:44