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 7845719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:20:20+00:00 2026-06-02T17:20:20+00:00

Im trying to write a search function to get an element in std::list by

  • 0

Im trying to write a search function to get an element in std::list by suing std:find. But im stuck in the third parameter in the find argorithm, regard to this guy How to search for an element in an stl list? I did overload the operator == pretty much but it seems still not working with the std::find.

This is my code:

class Node
{
    string word;
    int count;

    public:
        Node(string _word) :word(_word), count(0) {}
        ~Node() {}

        const string& getWord() const{
            return word;
        }
        bool operator == (const Node& other) const {
            return (this->word.compare(other.word) == 0);
        }
};
const Node &getNode(const list<Node> &nodes, const string &word){
    list<Node>::iterator itr;
    itr = find(nodes.begin(), nodes.end(), new Node(word)); <-- no viable overload '='
    return *itr;
}

I’m going very crazy with that issue now, please suggest me some hints. Thanks

  • 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-02T17:20:24+00:00Added an answer on June 2, 2026 at 5:20 pm

    To get the code working just remove the new from the sort
    call. However, this is not going to make your code any better.

    You don’t check if the element was actually found and just dereference
    the iterator. If the element wasn’t found, this is undefined behavior.

    Now, how to fix this.

    1. Don’t provide this function. If the user of Node has a list, she should be perfectly capable of calling std::sort himself.
    2. You don’t want to write the boiler-plate of wrapping and you don’t need to. Your class is convertible from std::string due to the single argument constructor taking a string (this should take a string by reference, though). So you can just write std::find(begin(nodes), end(nodes), "foobar");.
    3. You can also mark the constructor explicit (the converting behavior is not wanted most of the time) and then just add two free operator==(const Node&, const std::string&) and operator==(const std::string&, const Node&).

    In any case. Remove using namespace std; from your headers.

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

Sidebar

Related Questions

I'm trying to write search function, that gets its parameter from the user and
I'm trying to write a query for the find-as-you-type search bar. What I want
I am trying write a function that generates simulated data but if the simulated
I am trying to make search function with actionLink. the actionLink will give parameter
I'm trying to write a function to traverse a tree with depth first search.
I'm trying to write a very very simple script for a search function in
I'm trying to write a Javascript function to get the query string of the
I'm trying to write a function that will get me the attribute of a
I am trying to write a query to search a subclass attribute that is
I'm trying to write an SQL query that would search within a CSV (or

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.