Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting two characters passed by reference into a string?
    primarykey
    data
    text
    <p>Here's my program:</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 = 0; } 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+b; return(c); } string operator + (const Example&lt;char&gt; &amp;a, const Example&lt;char&gt; &amp;b) { string a1(""); a1+=a.data; a1+=b.data; return(a1); } 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> <p>How do I go about making the two characters into a string that I can return? I've tried multiple ways and I can't seem to get any of them to work. I think it may have something to do with the characters being passed by reference. </p> <p>EDIT: Ok so I got that specific function working with no problems. Now I got it compiled but before anything is displayed, there's a segmentation fault. Something is wrong with the addition for the the U data type. It'll add A and B and return AB, but it won't add 15 and 30. Also, I have to say thank you for all your help. I'm still new to programming and I really appreciate it.</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.
    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