Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use std::find/std::find_if with a vector of custom class objects?
    primarykey
    data
    text
    <p>I have a class representing a user called <code>Nick</code> and I want to use <code>std::find_if</code> on it, where I want to find if the userlist vector has an object included with the same username I pass in. I did a few attempts by trying to create a new <code>Nick</code> object for the username I want to test and overloading the <code>== operator</code> and then trying to use <code>find/find_if</code> on the object:</p> <pre><code> std::vector&lt;Nick&gt; userlist; std::string username = "Nicholas"; if (std::find(userlist.begin(), userlist.end(), new Nick(username, false)) != userlist.end())) { std::cout &lt;&lt; "found"; } </code></pre> <p>I have overloaded the <code>== operator</code> so comparing Nick == Nick2 should work, but the function returns <code>error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Nick' (or there is no acceptable conversion)</code>.</p> <p>Here is my Nick class for reference:</p> <pre><code>class Nick { private: Nick() { username = interest = email = ""; is_op = false; }; public: std::string username; std::string interest; std::string email; bool is_op; Nick(std::string d_username, std::string d_interest, std::string d_email, bool d_is_op) { Nick(); username = d_username; interest = d_interest; email = d_email; is_op = d_is_op; }; Nick(std::string d_username, bool d_is_op) { Nick(); username = d_username; is_op = d_is_op; }; friend bool operator== (Nick &amp;n1, Nick &amp;n2) { return (n1.username == n2.username); }; friend bool operator!= (Nick &amp;n1, Nick &amp;n2) { return !(n1 == n2); }; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload