Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ subscription operator overload
    primarykey
    data
    text
    <p>I'm working on brushing up on my C++ during some project downtime and have created a a Linked List project to do so. In this project I would like to return the current value at a set index. I've already got a method to do this, but want to work it out with an operator overload.</p> <p>To that end I did the necessary research to brush up and have created the following code (I won't subject everyone to my code and will only paste the pertinent sections):</p> <pre><code>T&amp; operator[](const int index); template&lt;class T&gt; T&amp; LinkedList&lt;T&gt;::operator[](const int index) { try { if(!isIndexValid(index)) throw ior; Node&lt;T&gt; *temp = _head; for(int i=1; i&lt;=index; i++) { temp = temp-&gt;Next; } return temp-&gt;Value; } catch(exception&amp; e) { cout &lt;&lt; e.what() &lt;&lt; endl; } } </code></pre> <p>In my main function I have the following line:</p> <pre><code>int foo = list[5]; </code></pre> <p>Everything looks good to me, but when I compile I get the following error:</p> <pre><code>error C2440: 'initializing' : cannot convert from 'LinkedList&lt;T&gt;' to 'int' </code></pre> <p>Does anyone have any ideas how to fix this?</p> <p>Thank you!</p> <p>For those that have asked, here is how the Node class is defined:</p> <pre><code>template&lt;class T&gt; class Node { public: Node(); Node(const T value); T Value; Node&lt;T&gt; *Prev; Node&lt;T&gt; *Next; }; </code></pre> <p>And here is the declaration for my list variable:</p> <pre><code>LinkedList&lt;int&gt; *list = new LinkedList&lt;int&gt;(); </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.
    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