Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:17:57+00:00 2026-06-08T10:17:57+00:00

I’m using a vector of pointers to free up a series of node objects

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:18:00+00:00Added an answer on June 8, 2026 at 10:18 am

    It look like your delete_nodes is a non-static member function. If so, you cannot just use delete_nodes as an argument for std::for_each. std::for_each requires a functor. Your delete_nodes is 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 (just delete_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_fun function. That will give you a binary functor, since std::mem_fun will turn the implicit this parameter into an explicit one. In order to turn it into a unary functor required by std::for_each you have to bind the first argument to a specific object pointer value (this probably?).

    The end result of the above steps will look as

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

    This is a unary functor that calls delete_nodes for this object.

    So, the for_each call in your example should look as follows

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

    However, it looks like in your implementation delete_nodes can 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 make delete_nodes static your code should work as is.

    Decide what path you wish to follow and make the necessary changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.