Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How to pass std::vector<void*> * as std::vector<MyStruct*>* to a function?

The function declaration is void SetDataReferences(std::vector<MyStruct*>* pVector);

if I pass std::vector<void*> * as std::vector<MyStruct*>* I am getting below error

error C2664: 'SetDataReferences' : cannot convert parameter 2 from 'std::vector<_Ty> *' to 'std::vector<_Ty> *'

How to resolve this?

What I have tried:

I dont know what is the alternative for it.
Posted
Updated 24-Sep-18 1:51am

Provided you are sure the vector you are passing actually contains pointer to valid MyStruct instances, a reinterpret_cast[^] would do the trick. Try, for instance:
C++
#include <iostream>
#include <vector>
using namespace std;

struct Foo
{
  string s;
  int i;
};


void reportFooVector( vector <Foo*> * pvf )
{
  for (auto p : *pvf)
    cout << p->s << ", " << p->i << endl;
}


int main()
{
  Foo foo{ "foo",  5};
  Foo goo{ "goo", 10};

  vector <void *> vf { &foo, & goo};

  reportFooVector( reinterpret_cast< vector < Foo * > * >(&vf));
}
 
Share this answer
 
The alternative is very clear: creating and passing the parameter in the right type.

You should take this warning very serious because it screams for a coding flaw.
C++
//TODO: Change the creation code of input parameter
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900