Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(<strong>Update</strong>: Here's a fully tested version on <a href="http://ideone.com/3KGU4" rel="nofollow">http://ideone.com/3KGU4</a>. For the <em>Additional question</em>, see <a href="http://ideone.com/w0dLo" rel="nofollow">http://ideone.com/w0dLo</a>)</p> <p>There is a difference between ordinary overloaded functions and template functions. For example, without any reference to templates a developer can declare:</p> <pre><code>void f(int x); void f(char *x); </code></pre> <p>Alternatively, a developer could use templates,</p> <pre><code>template &lt;class T&gt; void f(T x); </code></pre> <p>A major difference between them is that with ordinary functions, you must decide on a fixed set of allowed parameters in advance, and you must provide an implementation for each one. With templates, you can be more flexible.</p> <p>Later in your program, it is clear that you want <code>a</code> to be a template function, not simply an (overloaded) ordinary function. But when the compiler first sees mention of <code>a</code> (around line 10), it looks like it is declaring an ordinary function. To resolve this, you must take two steps. You must declare as soon as possible that <code>a</code> is a template function, so your first line should be:</p> <pre><code>template &lt;typename T&gt; int a(T&amp; x); </code></pre> <p>Then you must declare the relevant friendship. If <code>T</code> is <code>int</code>, then <code>a</code> takes a parameter of <code>test&lt;int&gt;&amp;</code>, not <code>int&amp;</code>. Therefore the two friend lines should be replaced with:</p> <pre><code>friend int a&lt;test&lt;T&gt; &gt;( test&lt;T&gt; &amp; x); // around line 10 friend int a&lt;test&lt;int&gt; &gt;( test&lt;int&gt; &amp; x); // around line 27 </code></pre> <p>and the specialization of <code>a</code> should be:</p> <pre><code>template &lt;&gt; int a&lt; test&lt;int&gt; &gt;(test&lt;int&gt;&amp; ) // around line 30 </code></pre> <p><strong>The Additional Question</strong></p> <p>Use <code>ostream</code> instead of <code>ofstream</code> (or else include <code>#include &lt;fstream&gt;</code> if you will output only to files and not to <code>cout</code>). In my answer, <code>operator &lt;&lt;</code> is not a template, but is a normal overloaded function. I'm not sure it's possible to have <code>operator&lt;&lt;</code> as a template. Also, I defined the operator at the place where it is declared and declared as a friend. To be honest, I think there are other, maybe better, ways but this worked for me.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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