I'm trying to pass a string to a function, which works fine when I use in Visual studio but I need run my code in linux with eclipse CDT as my IDE. The reason for choosing eclipse is I need to access matlab's engine.h in linux enviornment which is possible through eclipse.
I'm calling a function MputVar2d(x, "x");
where x
is a vector <vector<double> >
and "x"
is a string. and my function which is written in different header file is:
template <typename T>
void MputVar2d(const vector <vector<T> > &var, string name){
extern Engine *ep;
int row = var.size(), col = var[0].size();
mxArray *matlab = mxCreateDoubleMatrix(row, col, mxREAL);
T *pa = mxGetPr(matlab);
for (int j = 0; j < var[0].size(); j++){
for (int i = 0; i < var.size(); i++){
pa[i * row + j] = var[i][j];
}
}
engPutVariable(ep, name.c_str(), matlab);
}
I get the following error when I'm running my code:
Invalid arguments:
Candidates are:
void MputVar2d(const std::vector<std::vector<#0,std::allocator<#0>>,std::allocator<std::vector<#0,std::allocator<#0>>>> &, std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)
I tried executing my function as MputVar2d(x, string("x"));
, or defining string as std::string&
but it still gives the error.
The 2nd argument is a non-const string reference, judging by error message. You should make it const string& unless you are changing the string in the function and want the caller to see the change.
If you need more details, I will elaborate later today. For quick fix just change the function to take const string& name and you'll be fine.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments