Question

I am having trouble trying to insert a std::pair in the std::vector, with this code:

template <class R>
class AVectorContainner
{
public:
  AVectorContainner()
       {
         mVector= new std::vector<entry>;
       }
  typedef std::pair<int ,R *> entry;

  void insert(R* aPointer, int aID)
  {
     entry aEntry;
     aEntry=std::make_pair(aID,aPointer );
     mVector->push_back(aEntry);

  }
private: 
  std::vector<entry> * mVector;
}

This is the part of the main file, that I declare a pointer of a class and then I used it in the initialization of the template class.

In the main.cpp:

    int main()
    {
        SomeType * aTipe= new SomeType;
        int aID=1;
        AVectorContainer<SomeType> * aContainer= new AVectorContainer;
        aContainer->insert(aTipe,aId);//error line
delete aTipe;
delete aContainer;
        return 0;
    }

Compiler Output:

error: non-static reference member 'const int& std::pair<const int&, SomeType *>::first', can't use default assignment operator

error: value-initialization of reference type 'const int&'
Was it helpful?

Solution

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.

OTHER TIPS

Fixed all typos, compare the two ... he did like 100 in 20 lines!

#include <vector>
#include <utility>

template <class R>
class AVectorContainer
{
public:
  AVectorContainer()
       {
         mVector= new std::vector<entry>;
       }
  typedef std::pair<int ,R *> entry;

  void insert(R* aPointer, int aID)
  {
     entry aEntry;
     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= new AVectorContainer<SomeType>;
    aContainer->insert(aTipe,aID);//error line
    return 0;
}

After fixing all the typo's (Containner, aId, missing ; etc.) the code compiles just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top