userNew userNew - 2 months ago 20
C++ Question

vector to non vector assignment in cpp

I am getting error on

data *c=it;
. I need to retrieve only one value.

class X
{
string value4;
vector<data> *T1;
}
class data
{
string value1;
int value2;
}
void doTask(X V1)
{
vector<data> *tempdata=V1.getData();
for (std::vector<data>::iterator it = tempdata->begin() ; it !=tempdata->end(); ++it)
{
data *c=it;
sendData(value3,c);
}
}
void sendData(string s,data d)
{

}


I am getting this error:


error: cannot convert 'std::vector::iterator {aka
__gnu_cxx::__normal_iterator >}' to 'data*' in initialization


I am new in this vector usage. Can somebody help me in this?

Answer

You have to dereference the iterator to get a reference to the contained item type, then get its address to get a pointer:

data *c = &*it;
Comments