2011-03-28 51 views
2

我有一个DLL与类Test。 部首:matlab mex文件和C + + DLL(窗口)

 
class MY_EXPORT Test 
{ 
public: 
    int doit(const string &str); 
}; 

和来源:

 
int 
Test::doit(const string &str) 
{ 
    return int(str.length()); 
} 

现在我用它从MEX文件:

 
void 
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{ 
    string str("hello!"); 
    Test *t = new Test(); 
    t ->doit(str); 
} 

的问题,即变量str未正确传递给该方法doit。 它含有狂妄的方法。我发现这发生在通过引用传递的任何对象上。我做错了什么?请帮忙。 PS:如果我将声明更改为'int doit(const char *)',一切正常。

+0

哪个编译器? – 2011-03-28 14:50:50

+0

@David Heffernan:visual studio 2008(vc9) – Boris 2011-03-28 15:03:50

回答

4

问题是这样的:
libmex.dll(和整个Matlab的2010A/2010年b)使用Microsoft.VC80.CRT(版本= 8.0.50727.4053)
但您的Visual Studio使用Microsoft.VC90.CRT(版本= 9.0.21022.8)

如果你编写一个C++ mex文件,你需要在你的mex dll中使用相同版本的CRT库。 您可以免费安装Visual C++ 2005(SP1)Express Edition,并使用它编译mex文件。