Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The original poster failed to actually post the code that was causing the problem.</p> <p>He has since edited my post with the correct code that demonstrates the problem. The code that demonstrates the problem follows:</p> <pre><code>template &lt;class R, typename B=int&gt; class AVectorContainer { public: AVectorContainer() {} typedef R* ptr_Type; typedef const B &amp; const_ref_Type; typedef std::pair&lt;const_ref_Type ,ptr_Type&gt; entry; void insert(ptr_Type aPointer, const_ref_Type aID) { entry aEntry=std::make_pair(aID,aPointer); mVector.push_back(aEntry); } private: std::vector&lt;entry&gt; mVector; }; class SomeType { public: SomeType(){ x=5; } ~SomeType(){ } int x; }; int main() { SomeType * aTipe= new SomeType; int aID=1; AVectorContainer&lt;SomeType&gt; aContainer; aContainer.insert(aTipe,aID); return 0; } </code></pre> <p>Compiler output:</p> <pre><code>/usr/include/c++/4.7/bits/stl_pair.h:88: error: non-static reference member 'const int&amp; std::pair&lt;const int&amp;, SomeType*&gt;::first', can't use default assignment operator </code></pre> <p>The flaw is in these lines:</p> <pre><code> typedef R* ptr_Type; typedef const B &amp; const_ref_Type; typedef std::pair&lt;const_ref_Type ,ptr_Type&gt; entry; std::vector&lt;entry&gt; mVector; </code></pre> <p>Here, the original poster attempts to make a <code>vector</code> of <code>pair</code>s that contain a constant reference, and then does this:</p> <pre><code>entry aEntry; aEntry=std::make_pair(aID,aPointer ) </code></pre> <p>this attempts to assign one pair to another. But <code>const&amp;</code> variables cannot be assigned to another -- they can be constructed (initialized) from another <code>const&amp;</code>, but not assigned.</p> <p>An easy fix is:</p> <pre><code>entry aEntry=std::make_pair(aID,aPointer ) </code></pre> <p>so that we are not constructing <code>aEntry</code> from another <code>entry</code>, instead of default constructing <code>aEntry</code> (which is also illegal: <code>const&amp;</code> must be initialized), then assigning to it.</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