Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiler error trying to delete template type which is a pointer
    primarykey
    data
    text
    <p>I'm new to C++ and I have the ambition to understand how templates work. So I have implemented a generic list <code>MyList</code> which may contain both built-in primitive types and pointers. In the <code>remove</code> function I want to distinguish pointer types and built-ins so I can delete the object behind the pointer but leave the built-ins untouched. </p> <p>To distinguish the template types, which can be pointers or non-pointers, I wrote the following functions, which work fine:</p> <pre><code>// distinguish between pointer and non-pointer type of template variable template&lt;typename T&gt; bool is_pointer(T t) { return false; } template&lt;typename T&gt; bool is_pointer(T* t) { return true; } </code></pre> <p>In the list function <code>remove</code> the idea was to test for pointers and delete them in case. However, the delete statement does not compile:</p> <pre><code>template&lt;typename T&gt; void MyList&lt;T&gt;::remove() { ... if (is_pointer(temp-&gt;getContent())) { // delete object pointer points to T t = temp-&gt;getContent(); cout &lt;&lt; t; // prints out address // delete t; // produces compiler error (see below) } </code></pre> <p>In the <code>main.cpp</code> I test the list class with various types, I call amongst others:</p> <pre><code>MyList&lt;int&gt; mylist; // with type int mylist.remove(); mylist.add(3); // add and remove elements MyList&lt;string&gt; mylist2; // with type string ... MyList&lt;string*&gt; mylist3; // with type string* mylist.add(new string("three")); mylist.remove(); </code></pre> <p>When I comment out the statement <code>delete t;</code> I can verify that the control flow is correct: the if-statement is only entered for the <code>string*</code> example. However, if I uncomment the <code>delete</code> statement the compiler complains like that:</p> <pre><code>../mylist.h: In member function ‘void MyList&lt;T&gt;::remove() [with T = int]’: ../main.cpp:36:18: instantiated from here ../mylist.h:124:6: error: type ‘int’ argument given to ‘delete’, expected pointer ../mylist.h: In member function ‘void MyList&lt;T&gt;::remove() [with T = std::basic_string&lt;char&gt;]’: ../main.cpp:71:18: instantiated from here ../mylist.h:124:6: error: type ‘struct std::basic_string&lt;char&gt;’ argument given to ‘delete’, expected pointer make: *** [main.o] Error 1 </code></pre> <p>What is it that I don't see? I'm using the <code>delete</code> statement on pointers only but still I get these compiler errors. If I print out <code>t</code> in the if-statement it is a pointer address!</p>
    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.
 

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