To start off, i'm quite the noob at c++, and i'm lost. I've googled for hours but I think my problem is either niche or too easy. Here goes:
I'm writing a c++ mex file to be used from matlab. However, I would like to allow both single as double precision inputs. So I want to use templates to achieve this.
I've this class:
template< typename T > class img {
T *_data;
public:
img (T *_data) : _data(_data) {}
T &operator[]( int i ) { return _data[ i ]; }
}
Then in the main.cpp:
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
if (mxIsDouble(prhs[0])) {
double *f = (double *)mxGetPr( prhs[ 0 ] );
img<double> myimg(f);
} else if (mxIsSingle(prhs[0])) {
float *f = (float *)mxGetPr( prhs[ 0 ] );
img<float> myimg(f);
}
mexPrintf( "value of myimg[%d] = %f\n", 10, myimg[10] );
}
Now the compiler complains about "error: use of undeclared identifier myimg".
I think this is because the object myimg
is only existing inside the if
scope, right?
If I define the img<double> myimg(f);
before the if
statement it works, but obviously only for doubles.
Help
P.S. I've found this related stack question: Can a MATLAB Mex function accept both single and doubles?