Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - No match for 'operator=' when compiling using GCC
    text
    copied!<p>I recently tried building my own shared and weak pointers. Code that compiles using Visual Studio doesn't compile in GCC (4.5.0) with the following error:</p> <pre><code>main.cpp: In function 'int main()': main.cpp:18:27: error: no match for 'operator=' in 'wp1 = weak_ptr&lt;int&gt;(((const shared_ptr&lt;int&gt;&amp;)((const shared_ptr&lt;int&gt;*)(&amp; sp1))))' weak_ptr.h:59:9: note: candidate is: void weak_ptr&lt;T&gt;::operator=(weak_ptr&lt;T&gt;&amp;) [with T = int, weak_ptr&lt;T&gt; = weak_ptr&lt;int&gt;] </code></pre> <p><em>Here are the most important parts of my code:</em></p> <p>1) Weak pointer implementation (note the declaration of <code>operator=</code>)</p> <pre><code>#include "smart_ptr_wrapper.hpp" #include "shared_ptr.h" template &lt;typename T&gt; class weak_ptr { private: // Weak wrapper implementation typedef smart_ptr_wrapper&lt;T&gt; weak_ptr_wrapper; weak_ptr_wrapper* wrapper; private: // Shared wrapper additional routines void increase_reference_count() { ++(wrapper-&gt;weak_count); } void decrease_reference_count() { --(wrapper-&gt;weak_count); // Dispose the wrapper if there are no more // references to this object // @note This should actually lock the wrapper to // preserve thread safety if (wrapper-&gt;strong_count == 0 &amp;&amp; wrapper-&gt;weak_count == 0) { delete wrapper; } } public: // Default constructor to grant syntax flexibility weak_ptr() : wrapper(NULL) { } weak_ptr(const shared_ptr&lt;T&gt;&amp; pointer) : wrapper(pointer.wrapper) { increase_reference_count(); } weak_ptr(const weak_ptr&amp; p) : wrapper(p.wrapper) { increase_reference_count(); } weak_ptr&amp; operator= (weak_ptr&amp; p) { // Set new reference counts // @note If this is 'just-a-pointer', which was created // using default constructor then our wrapper would be 'NULL' if (wrapper != NULL) { decrease_reference_count(); } p.increase_reference_count(); // Set new wrapper wrapper = p.wrapper; return *this; } ~weak_ptr() { decrease_reference_count(); } T* get() const { return (wrapper-&gt;strong_count == 0) ? NULL: wrapper-&gt;raw_pointer; } T* operator-&gt; () const { return get(); } T&amp; operator* () const { return *get(); } // User comparison operation operator void* () const { return (get() == NULL); } }; </code></pre> <p>2) main.cpp</p> <pre><code>int main() { shared_ptr&lt;int&gt; sp1(new int(4)); weak_ptr&lt;int&gt; wp1(sp1); // Next line can't be compiled by gcc... Why? wp1 = weak_ptr&lt;int&gt;(sp1); return 0; } </code></pre> <h1>Q:</h1> <p>Why does this happen? I'm probably plain stupid, but I can't see what's wrong with this code and can't undestand GCC behaviour. I would also appreciate if someone could explain why does this code compile and why does it work under MSVS (I mean, why would one compiler do it fine and why would the second fail). Thank you.</p> <p>Update: Full code and compiler error can be seen here - <a href="http://codepad.org/MirlNayf" rel="nofollow noreferrer">http://codepad.org/MirlNayf</a></p>
 

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