[Solved] std::vector inserting std::pair


The original poster failed to actually post the code that was causing the problem.

He has since edited my post with the correct code that demonstrates the problem. The code that demonstrates the problem follows:

template <class R, typename B=int>
class AVectorContainer
{
public:
  AVectorContainer() {}
  typedef R* ptr_Type;
  typedef const B & const_ref_Type;
  typedef std::pair<const_ref_Type ,ptr_Type> entry;


  void insert(ptr_Type aPointer, const_ref_Type aID) {
     entry aEntry=std::make_pair(aID,aPointer);
     mVector.push_back(aEntry);
  }
private:
  std::vector<entry> mVector;
};

class SomeType
{
public:
  SomeType(){ x=5; }
  ~SomeType(){ }
  int x;
};

int main()
{
  SomeType * aTipe= new SomeType;
  int aID=1;
  AVectorContainer<SomeType> aContainer;
  aContainer.insert(aTipe,aID);
  return 0;
}

Compiler output:

/usr/include/c++/4.7/bits/stl_pair.h:88: error: non-static reference member 'const int& std::pair<const int&, SomeType*>::first', can't use default assignment operator

The flaw is in these lines:

  typedef R* ptr_Type;
  typedef const B & const_ref_Type;
  typedef std::pair<const_ref_Type ,ptr_Type> entry;
  std::vector<entry> mVector;

Here, the original poster attempts to make a vector of pairs that contain a constant reference, and then does this:

entry aEntry;
aEntry=std::make_pair(aID,aPointer )

this attempts to assign one pair to another. But const& variables cannot be assigned to another — they can be constructed (initialized) from another const&, but not assigned.

An easy fix is:

entry aEntry=std::make_pair(aID,aPointer )

so that we are not constructing aEntry from another entry, instead of default constructing aEntry (which is also illegal: const& must be initialized), then assigning to it.

5

solved std::vector inserting std::pair