C++ 错误C2040:&x27;MyClass';:';void*';与';MyClass';
我用C++编写了一个DLL的C_包装器(C++)。 头文件C++ 错误C2040:&x27;MyClass';:';void*';与';MyClass';,c++,C++,我用C++编写了一个DLL的C_包装器(C++)。 头文件 #define DLLIMPORT __declspec (dllexport) #ifdef __cplusplus extern "C" { #endif typedef void* MyClass; DLLIMPORT MyClass* createWrapper(double a, double b); #ifdef __cplusplus } #endif 源文件: #include "stdafx.h" #include
#define DLLIMPORT __declspec (dllexport)
#ifdef __cplusplus
extern "C" {
#endif
typedef void* MyClass;
DLLIMPORT MyClass* createWrapper(double a, double b);
#ifdef __cplusplus
}
#endif
源文件:
#include "stdafx.h"
#include "MyClass.h"
#include "C_DllWrapper.h"
DLLIMPORT MyClass* createWrapper(double a, double b)
{
return new MyClass(a,b);
}
我收到以下错误消息:
错误C2040:“MyClass”:“void*”与“MyClass”的间接寻址级别不同
我已更改了我的C_包装: 头文件:
#ifdef __cplusplus
#endif
extern "C"__declspec (dllexport) void* createWrapper(double a, double b);
extern "C"__declspec (dllexport) void destoryWrapper(void* instance);
extern "C"__declspec (dllexport) double Add(void* instance, double a, double b);
#ifdef __cplusplus
#endif
源文件:
#include "stdafx.h"
#include "MyClass.h"
#include "C_DllWrapper.h"
extern "C"__declspec (dllexport) void* createWrapper(double a, double b)
{
return new MyClass(a,b);
}
extern "C"__declspec (dllexport) void destoryWrapper(void *instance)
{
delete static_cast<MyClass*>(instance);
}
extern "C"__declspec (dllexport) double Add(void *instance, double a, double b)
{
MyClass *myClass = static_cast<MyClass*>(instance);
return myClass->Add(a, b);
}
这是在c++中,但什么是在Labview中。
如何为所有剩余的函数使用该对象“void*testref”
对不起,我的英语您的
typedef
正在破坏您的代码。你不需要它。typedef导致编译器在看到您将MyClass
重新定义为void*
的newmyclass
时感到困惑
typedef void*MyClass
行createWrapper
函数的签名更改为void*createWrapper
extern“C”
添加到createWrapper
函数定义中,而不仅仅是其声明如果你正在开发一个C API,它是一个对象的方法的一个平面表示,考虑采用一个OOP ESQY命名约定(“<代码> NoNovie< /Cord>”,而不是使用<代码> VerbNoun <代码>名称),因为当检查代码中所有函数的列表时,它们会更有组织。 另外,请不要忘记定义一个函数,该函数可
删除
或以其他方式释放对象实例:
extern "C" void* MyClass_create() {
return new MyClass();
}
extern "C" void MyClass_destroy(void* instance) {
delete static_cast<MyClass*>(instance);
}
extern“C”void*MyClass_create(){
返回新的MyClass();
}
外部“C”void MyClass_destroy(void*实例){
删除静态_cast(实例);
}
我建议阅读此QA以了解更多信息和大量有用的示例:
我还怀疑您使用的
DLLIMPORT
不正确,因为我相信您实际上是在导出这些函数。我觉得直接使用\uu declspec
比使用宏更好。语法是\uuu declspec(dllexport)
,请参见此处:您能在单个文件中复制它吗?另外,错误具体在哪里?您确实将类型别名MyClass
设置为void*
,您认为当您这样做时会发生什么,例如new MyClass(…)
?还要注意,代码中的createWrapper
声明为返回指向指针的指针!
extern "C"__declspec (dllexport) void* createWrapper(double a, double b)
{
return new MyClass(a,b);
}
extern "C"__declspec (dllexport) void destoryWrapper(void *instance)
{
delete static_cast<MyClass*>(instance);
}
extern "C"__declspec (dllexport) double Add(void *instance, double a, double b)
{
MyClass *myClass = reinterpret_cast<MyClass*>(instance);
return myClass->Add(a, b);
}
extern "C"__declspec (dllexport) double Add(void *instance, double a, double b)
{
MyClass *myClass = static_cast<MyClass*>(instance);
return myClass->Add(a, b);
}
int main ()
{
void * testref;
testref = create (1.2);
Add (testref, 5.6);
......
}
extern "C" void* MyClass_create() {
return new MyClass();
}
extern "C" void MyClass_destroy(void* instance) {
delete static_cast<MyClass*>(instance);
}