Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest thing that would work:</p> <pre><code>string operator + (const Example&lt;char&gt; &amp;a, const Example&lt;char&gt; &amp;b) { return { a.data, b.data }; } </code></pre> <p>If you have an 'older' compiler:</p> <pre><code>string operator + (const Example&lt;char&gt; &amp;a, const Example&lt;char&gt; &amp;b) { char both[] = {a.data, b.data}; return string(both, both+2); } </code></pre> <p>See it live: <a href="http://liveworkspace.org/code/2ORW8E%240" rel="nofollow">http://liveworkspace.org/code/2ORW8E$0</a></p> <p><strong>UPDATE</strong> To the edit question: There is also a big problem (infinite recursion) here:</p> <pre><code>template &lt;class U&gt; U operator + (const Example&lt;U&gt; &amp;a, const Example&lt;U&gt; &amp;b) { U c; c = a+b; // NEEDS TO BE a.data + b.data; return(c); } </code></pre> <p>Here is a fixed version:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; template &lt;class T&gt; class Example { private: T data; public: Example() : data() { } void setData(T elem) { data = elem; } template &lt;class U&gt; friend ostream&amp; operator &lt;&lt; (ostream &amp;, const Example&lt;U&gt;&amp;); friend ostream&amp; operator &lt;&lt; (ostream &amp;, const Example&lt;char&gt;&amp;); friend string operator + (const Example&lt;char&gt; &amp;, const Example&lt;char&gt; &amp;); template &lt;class U&gt; friend U operator + (const Example&lt;U&gt; &amp;, const Example&lt;U&gt; &amp;); }; template &lt;class U&gt; U operator + (const Example&lt;U&gt; &amp;a, const Example&lt;U&gt; &amp;b) { U c; c = a.data+b.data; return(c); } string operator + (const Example&lt;char&gt; &amp;a, const Example&lt;char&gt; &amp;b) { char both[] = {a.data, b.data}; return string(both, both+2); } template &lt;class T&gt; ostream&amp; operator &lt;&lt; (ostream &amp;o, const Example&lt;T&gt; &amp;t) { o &lt;&lt; t.data; return o; } ostream&amp; operator &lt;&lt; (ostream &amp;o, const Example&lt;char&gt; &amp;t) { o &lt;&lt; "'" &lt;&lt; t.data &lt;&lt; "'"; return o; } int main() { Example&lt;int&gt; tInt1, tInt2; Example&lt;char&gt; tChar1, tChar2; tInt1.setData(15); tInt2.setData(30); tChar1.setData('A'); tChar2.setData('B'); cout &lt;&lt; tInt1 &lt;&lt; " + " &lt;&lt; tInt2 &lt;&lt; " = " &lt;&lt; (tInt1 + tInt2) &lt;&lt; endl; cout &lt;&lt; tChar1 &lt;&lt; " + " &lt;&lt; tChar2 &lt;&lt; " = " &lt;&lt; (tChar1 + tChar2) &lt;&lt; endl; return 0; } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. 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