如何在结构体中传递指向CURL的指针到向量

How to pass pointer to CURL in struct to vector?

本文关键字:CURL 指针 向量 结构体      更新时间:2023-10-16
CURL * myHandle;
(... some operations)
std::vector<curl_a> * crl = &program_data::getInstance().curl_acc[80];
crl->push_back({ myHandle,true }); //look at bottom

push_back的struct似乎:

struct curl_a {
    curl_a(){};
    curl_a(CURL * d, int &l) :data(d), logged(l){};
    CURL * data;
    bool logged;
};

//header.h(单例模式)

std::unordered_map <int, std::vector<curl_a>> curl_acc;

现在告诉我,为什么push_back不起作用?编译器从struct .h返回错误

curl_a' : illegal member initialization: 'data' is not a base or member

问题似乎是您正在使用无效的第二个参数调用curl_a构造函数。当参数明确需要一个l值(引用)时,您正在传递true

初始化时不使用值true,而是使用整型变量。

int x = 1;
crl->push_back({ myHandle, x }); 

这是你的例子,但简化使用int*而不是CURL*: http://ideone.com/SzxESi

下面是一个使用true作为参数的失败尝试的例子:http://ideone.com/HQtAaU