Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloading function doesn't work with complex
    text
    copied!<p>I have written this simple C++ code to swap any two variables. It works for int and double, but doesn't work for complex numbers, any idea why?</p> <pre><code>#include &lt;iostream&gt; #include &lt;complex&gt; using std::cout; using std::endl; using std::complex; template &lt;class T&gt; inline void swap(T&amp; d, T&amp; s) { T temp = d; d = s; s = temp; } int main() { int m=5, n=10; double x=5.3, y=10.6; complex&lt;double&gt; r(2.4, 3.5), s(3.4, 6.7); cout &lt;&lt; "inputs: " &lt;&lt; m &lt;&lt; " , " &lt;&lt; n &lt;&lt; endl; swap(m, n); cout &lt;&lt; "outputs: " &lt;&lt; m &lt;&lt; " , " &lt;&lt; n &lt;&lt; endl; cout &lt;&lt; "double inputs: " &lt;&lt; x &lt;&lt; " , " &lt;&lt; y &lt;&lt; endl; swap(x, y); cout &lt;&lt; "double outputs: " &lt;&lt; x &lt;&lt; " , " &lt;&lt; y &lt;&lt; endl; cout &lt;&lt; "complex inputs: " &lt;&lt; r &lt;&lt; " , " &lt;&lt; s &lt;&lt; endl; swap(r, s); cout &lt;&lt; "complex outputs: " &lt;&lt; r &lt;&lt; " , " &lt;&lt; s &lt;&lt; endl; } </code></pre> <p>This is the error:</p> <pre><code>g++ 02.swap.template.cpp -o c.out.02 02.swap.template.cpp: In function ‘int main()’: 02.swap.template.cpp:37:13: error: call of overloaded ‘swap(std::complex&lt;double&gt;&amp;, std::complex&lt;double&gt;&amp;)’ is ambiguous 02.swap.template.cpp:37:13: note: candidates are: 02.swap.template.cpp:13:13: note: void swap(T&amp;, T&amp;) [with T = std::complex&lt;double&gt;] /usr/include/c++/4.6/bits/move.h:122:5: note: void std::swap(_Tp&amp;, _Tp&amp;) [with _Tp = std::complex&lt;double&gt;] </code></pre>
 

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