0

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

1 Answer 1

8

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I can't drop the const due to complicated reasons, other stuff is going on in the function. And I can't change the vector type. Is there any other way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.