标准::make_pair: "cannot convert ‘int*’ to ‘std::pair<EndPointAddr*, EndPointAddr*>*’ in initial

std::make_pair: "cannot convert ‘int*’ to ‘std::pair<EndPointAddr*, EndPointAddr*>*’ in initialization"

本文关键字:EndPointAddr pair gt initial in lt int cannot 标准 make std      更新时间:2023-10-16

在下面列出的函数中,我试图创建一个指向std::pair对象的指针,在此消息的底部显示编译错误(相关的行号已在下面的代码片段中标记)。你能告诉我这个代码有什么问题吗?谢谢。

 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); 
 }  
编译输出:

 ../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 ‘;’
std::pair<EndPointAddr*, EndPointAddr* > *thePair = 
  new  std::make_pair(ptrRequesterServiceAddr, ptrRequestedServiceAddr);
            ^^^^^^
应该

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

std::make_pair是一个函数。

您正在尝试将std::pair指针分配给函数。

您需要使用constructor而不是make pair而不是std::make_pair

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

您的示例代码没有显示mServiceSubscriptionsList的类型,这将有助于了解。

试试这个:

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); 
 } 

相关文章: