Note that there are some explanatory texts on larger screens.

plurals
  1. POuse operators in templates in c++
    primarykey
    data
    text
    <p>I am trying to implement a List class using pointers and am trying to implement a function LOCATE(T x) where T is for the template and returns the first position of the element x if found, else returns last position + 1.</p> <p>My functions code is </p> <pre><code>template&lt;class T&gt; int List&lt;T&gt;::locate(T n) const { int size = end(); Node&lt;T&gt; * p = head_; for (int i = 0; i &lt; size; i++) { if (p-&gt;data() == n) // fails on this line return i; p = p-&gt;link(); } return size; // if no match found } </code></pre> <p>I initialise my list with T as string as </p> <pre><code>List&lt;string&gt; myList; </code></pre> <p>but I get an error message</p> <p>'bool std::operator ==(const std::istreambuf_iterator&lt;_Elem,_Traits> &amp;,const std::istreambuf_iterator&lt;_Elem,_Traits> &amp;)' : could not deduce template argument for 'const std::istreambuf_iterator&lt;_Elem,_Traits> &amp;' from 'std::string</p> <p>Why is the error coming up even though the '==' operator is defined for the string class? '</p> <p>The code for Node is </p> <pre><code>template&lt;typename T&gt; class Node { public: // Constructors Node(); Node(T d, Node&lt;T&gt; * l = NULL); //Inspectors T data() const; Node&lt;T&gt; * link() const; // Mutators void data(T d); // assigns new value to Node void link(Node&lt;T&gt; * l); // points this Node to a different one // Destructor ~Node(); private: Node&lt;T&gt; * link_; T data_; }; template&lt;typename T&gt; T Node&lt;T&gt;::data() const { return data_; } template&lt;typename T&gt; Node&lt;T&gt;* Node&lt;T&gt;::link() const { return link_; } </code></pre> <p>The calling code is </p> <pre><code>List&lt;string&gt; test; test.add("abc"); cout &lt;&lt; test.locate("abc") &lt;&lt; endl; </code></pre>
    singulars
    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.
    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