I am not sure how to fix this problem without creating an extra variable. Here is the code that does not compile:
std::string & printVec(std::vector<double> const &ds, std::string &dum) { std::vector<double>::iterator it; // std::vector<double> dsi = ds; // Created just to get the code to work, using dsi.begin() etc. dum = " "; for (it = ds.begin(); it != ds.end(); it++) { // Compiler error. No suitable "=" dum = dum + std::to_string(*it) + " "; } return dum; } If I remove the const on the input, it compiles:
std::string & printVec(std::vector<double> &ds, std::string &dum) { std::vector<double>::iterator it; dum = " "; for (it = ds.begin(); it != ds.end(); it++) { dum = dum + std::to_string(*it) + " "; } return dum; } I have reasons to want the const. What is a way to get the same functionality but not remove the const?
std::vector<double>::iterator->std::vector<double>::const_iterator.for (auto it = ds.begin(); it != ds.end(); it++)orfor (double d : ds) { dum = dum + std::to_string(d) + " "; }iteratorcan be used to change the element of a vector, so cannot be used on aconstvector. Useconst_iteratorinstead.