Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ resizing array using generics / templates
    primarykey
    data
    text
    <p>We have just been introduced to generics and templates in my comp sci class. I have been asked to create a generic container class that supports storage and retrieval of any data type. I have all of my functions working okay, except when it comes to resize my array. I have the call for resize inside my insert function:</p> <pre><code>template&lt; typename T &gt; void Container&lt; T &gt;::insert( T magic) { if (index == size) { resize(); } containerPtr[index] = magic; index++; } </code></pre> <p>The size variable is the size of the array and the index is the next insert location.</p> <p>and here is my resize function:</p> <pre><code>template&lt; typename T &gt; void Container&lt; T &gt;::resize() { int doubSize = size * 2; Container&lt; T &gt; temp(doubSize); for (int i = 0; i &lt; size; i++) { temp[i] = containerPtr[i]; // error 1 here } *containerPtr = temp; // error 2 here size = doubSize; } </code></pre> <p>My overloaded =</p> <pre><code>template&lt; typename T &gt; const T Container&lt; T &gt;::operator=(const T&amp; rhs) { if(this == &amp;rhs) { return *this; } return *rhs; } </code></pre> <p>I receive the following errors when I try to compile:</p> <pre><code>1: error C2676: binary '[': 'Container&lt;T&gt;' does not define this operator or a conversion to a type acceptable to the predefined operator 2: error C2679: binary '=': no operator found which takes a right-hand operand of type 'Container&lt;T&gt;' (or there is no acceptable conversion) </code></pre> <p>I am not sure where I am going wrong here...</p>
    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.
 

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