So, I'm creating a class that represents an arbitrarily-sized matrix, and I want to write a function that "exports" the internal Vector to a 2-dimensional array.
I've found some code that seems to do what I want. Here is what the code looks in my class:
// Export matrix to array
double** Matrix::a_data() const
{
vector<double*> ptrs;
for (auto& vec : mat)
{
ptrs.push_back(vec.data());
}
return ptrs.data();
}
where mat is the vector < vector < double > > used to store the matrix's entries.
The post in question stated that vec needed to be an auto& instead of an auto because it is "very important to prevent vec from being a temporary copy."
However, in Visual Studio I'm always getting an error "no instance of overloaded function push_back matches the argument list... argument types are (const double*)"
So, my question is, how can I get this to work without changing auto& to auto? Also, how would one extend this to 3 dimensions?
ptrs.data()is a dangling pointer as soon asptrsis destroyed, which happens whena_data()returns. - aschepler