简体   繁体   中英

assign to std:vector from a structure containing an array

I am trying to assign data from a struct to a std::vector

Here is the code

struct myArray
{
   double * data;
   size_t   len;
};


typedef std::vector<double>  DoubleVect;


DoubleVect myvect;
MyArray myarr;

// code to initialize alloc and populate the MyArray variable
// ....


myvect.assign(&myarr.data, &myarr.data + myarr.len);  // compiler barfs here ...

Any idea why? and how may I fix this?

Yes. Get rid of the address operator and it will be fine. Taking the address of the data member data gives you an expression of type double** . This is certainly not what you want.

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