简体   繁体   中英

c++ vector push_back with object pointer

I need to create a vector of object pointers by having an object passed into this function and then put into a vector of object pointers with push_back, but I'm getting "invalid conversion from const Person* to std::vector::value_type {aka Person*}" How do I pass the pointer to push_back correctly to make this work?

vector<Person*>vptr;

void insert(const Person&p)
{
    const Person*ptr=&p;
    vptr.push_back(ptr);
}

You would have to drop the const from the function parameter list and pointer declaration:

void insert(Person& p)
{
  vptr.push_back(&p);
}

or store const Person* :

vector<const Person*> vptr;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM