0

In below listed function, I am trying create a pointer to an std::pair object where I get a compilation error shown at the bottom of this message (The related line number has been marked in the code snippet below). Could you please tell me what's wrong with this code? Thanks.

 void 
 XXX::addSubscription(EndPointAddr* ptrRequesterServiceAddr, 
                      EndPointAddr* ptrRequestedServiceAddr)
 {
  //LINE 908 indicated in the g++ output starts HERE
  std::pair<EndPointAddr*, EndPointAddr* > *thePair = 
  new  std::make_pair(ptrRequesterServiceAddr, ptrRequestedServiceAddr);

  mServiceSubscriptionsList.push_back(thePair); 
 }  

Compilation Output:

 ../XXX.cpp: 
 In member function ‘void XXX::addSubscription(EndPointAddr*, EndPointAddr*)’:
 ../XXX.cpp:908:59: error: expected type-specifier
 ../XXX.cpp:908:59: error: cannot convert ‘int*’ to ‘std::pair<EndPointAddr*, EndPointAddr*>*’ in initialization
 ../XXX.cpp:908:59: error: expected ‘,’ or ‘;’
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60
  • Why do you need to create a dynamic pair object. Why not use std::pair thePair; and then thePair = std::make_pair(ptrRequesterServiceAddr, ptrRequestedServiceAddr); And the mServiceSubscriptionsList.push_back(&thePair); – Vaibhav Desai Apr 11 '13 at 17:36
  • @VaibhavDesai, because `thePair` will be destroyed after the function ends. So, you will have a dangling pointer. – awesoon Apr 11 '13 at 17:39
  • ^ Thank you. Missed it completely. – Vaibhav Desai Apr 11 '13 at 17:41
  • @H2CO3 "Hungarian notation is hard to read. – H2CO3" Says a native Hungarian. :) – Ali Apr 11 '13 at 19:25
  • @Ali Yeah, even with my Hungarian nationality, I find that convention brainless ;-) –  Apr 11 '13 at 19:26
  • @Ali Well. That's for Fortran programmers, it's not C++-esque, is it? (Keblemre, barátom! :D Dobj egy e-mailt!) –  Apr 11 '13 at 19:36

3 Answers3

2
std::pair<EndPointAddr*, EndPointAddr* > *thePair = 
  new  std::make_pair(ptrRequesterServiceAddr, ptrRequestedServiceAddr);
            ^^^^^^

should be

std::pair<EndPointAddr*, EndPointAddr* > *thePair = 
  new  std::pair<EndPointAddr*, EndPointAddr*>(ptrRequesterServiceAddr, ptrRequestedServiceAddr);
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

std::make_pair is a function. You are trying to assign an std::pair pointer to a function.

1

You need to use constructor instead of make pair instead of std::make_pair

  std::pair<EndPointAddr*, EndPointAddr* > *thePair = 
  new  std::pair<EndPointAddr*, EndPointAddr* >(ptrRequesterServiceAddr, ptrRequestedServiceAddr);
alexrider
  • 4,449
  • 1
  • 17
  • 27
0

Your example code does not show the type of mServiceSubscriptionsList which would be helpful to know.

Try this:

void 
 XXX::addSubscription(EndPointAddr* ptrRequesterServiceAddr, 
                      EndPointAddr* ptrRequestedServiceAddr)
 {
  //LINE 908 indicated in the g++ output starts HERE
  std::pair<EndPointAddr*, EndPointAddr* > thePair = 
  std::make_pair(ptrRequesterServiceAddr, ptrRequestedServiceAddr);

  mServiceSubscriptionsList.push_back(thePair); 
 } 
ChetS
  • 658
  • 7
  • 15