포인터의 벡터를 사용하여 힙의 일련의 노드 객체를 확보하고 있습니다. 벡터에는 모든 노드 객체 주소가 있으며 for_each 루프와 함께 벡터의 모든 노드를 삭제하는 데 사용되는 delete_nodes 함수가 있습니다. 어떤 이유로 나는의 for_each 루프와 이클립스 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>)'
코드는 허프만 코딩, 그리고의 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
@jogojapan 내가 아는 한 다른 delete_nodes가 없습니다. 또한 delete_nodes의 이름을 다른 것으로 변경하면 오류가 계속 발생합니다. –
Btw 나는'delete_nodes' 함수가 정적 멤버 함수로 정의되어 있다고 가정했습니다. 실제로 맞습니까? 그렇지 않다면 아래의 Matteo Italia가 맞습니다 (비록 컴파일러가 오해의 소지가있는 오류 메시지를 발견 할지라도). – jogojapan