-10

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&'
Ricardo_arg
  • 520
  • 11
  • 28

3 Answers3

2

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;
}
gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17
2

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • True, I didn't pay much attention to the actual code. I was too busy `SnD'ing` the typos! – gifnoc-gkp Aug 15 '13 at 19:25
  • seriously?? this is a solution?? just checking the types and adding some class that was just writting for showing proposing?. – Ricardo_arg Aug 15 '13 at 21:03
  • @Ricardo_arg it wasn't much of a question. Your code was full of typos -- TheOtherGuy fixed that. I noticed you also had a pile of unnecessary `new`s: rather than editing TheOtherGuy's answer to remove them, I tossed it out here. If ThatOtherGuy improved his answer, I'd just delete mine. – Yakk - Adam Nevraumont Aug 15 '13 at 21:32
  • here is a similar post: http://stackoverflow.com/questions/634662/non-static-const-member-cant-use-default-assignment-operator, this one is facing the same problem. one of the comment to that question was this: http://blog.copton.net/archives/2007/10/13/stdvector/index.html. The code doesnt compile, so fixit filling with semicolons and other isses doesnt seemt to improve the quality of the question, the compiler again is ringing with the same output, sorry but those anwser doesnt respond the question. – Ricardo_arg Aug 15 '13 at 21:50
  • The code I wrote compiles. I even link to a live example. If yours does not, maybe your compiler us broken, or maybe your code is different than mine. @ricardo_arg – Yakk - Adam Nevraumont Aug 15 '13 at 22:28
0

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

nullptr
  • 11,008
  • 1
  • 23
  • 18