1

I am testing to call another function in my mex file. But I came across a building error. " "void __cdecl transferarray1(struct mxArray_tag const *,class std::vector > &)" (??$transferarray1@H@@YAXPEBUmxArray_tag@@AEAV?$vector@HV?$allocator@H@std@@@std@@@Z) test.mexw64 : fatal error LNK1120" My code is below: test.cpp:

#include "mex.h"
#include <vector>
#include "inoutmex.h"
using namespace std;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
vector<int> idx;
transferarray1(prhs[0],idx);
}

inoutmex.cpp

#include "mex.h"
#include <vector>
using namespace std;

template <typename T> 
void transferarray1(const mxArray *ix, vector<T>&V)
{
    mwSize num = mxGetNumberOfElements(ix);
    mxDouble *ptr;
    ptr = mxGetPr(ix);
    for (int i = 0; i < (int)num; i++)
    {
        V.push_back(T(*ptr++));
    }
}

inoutmex.h

#include "mex.h"
#include <vector>
using namespace std;
template <typename T> 
void transferarray1(const mxArray *ix, vector<T>&V);

These 3 files are all under one directory. I think it must be related to link problem. I ran the command mex test.cpp in matlab command line. It gives the link error. Any suggestion will be appreciate.

Wei Zhang
  • 325
  • 2
  • 16
  • You need to instantiate the template for the type you intend to use it with. See the linked Q&A for ways to do so. -- As a side-comment (not related to your problem): you should always include the header file in the source file that implements those functions. That is, `inoutmex.cpp` should include `inoutmex.h`. This allows the compiler to give error messages if the function signatures don't match. – Cris Luengo Oct 17 '19 at 16:01

0 Answers0