Class 从Interface.cll初始化
我正在尝试实现接口类的类实例。使用project explorer浏览接口(.NET DLL),它显示:Class 从Interface.cll初始化,class,interface,c++-cli,Class,Interface,C++ Cli,我正在尝试实现接口类的类实例。使用project explorer浏览接口(.NET DLL),它显示: bool CreateInstance(SharedLibrary::MemoryArbiter^ pntMemory, SharedLibrary::clsMessageQueue^ pntMessageQueue, SharedLibrary::clsGPIO^ pntGPIO,
bool CreateInstance(SharedLibrary::MemoryArbiter^ pntMemory,
SharedLibrary::clsMessageQueue^ pntMessageQueue,
SharedLibrary::clsGPIO^ pntGPIO,
SharedLibrary::Types^ pntProgramSettings,
SharedLibrary::DisplayDriver^ pntDisplayDriver)
Member from Plugin_Interface::IPlugin
但是如果我在我的课堂上写作。h:
using namespace System;
using namespace System::ComponentModel;
using namespace SharedLibrary;
namespace MyCppPlugin {
[AttributeUsageAttribute(AttributeTargets::Class | AttributeTargets::Method |
AttributeTargets::Property | AttributeTargets::Field,
AllowMultiple = true, Inherited = false)]
ref class MyPlugin abstract : public Plugin_Interface::IPlugin
{
bool CreateInstance(SharedLibrary::MemoryArbiter^ pntMemory,
SharedLibrary::clsMessageQueue^ pntMessageQueue,
SharedLibrary::clsGPIO^ pntGPIO, SharedLibrary::Types^
pntProgramSettings, SharedLibrary::DisplayDriver^ pntDisplayDriver);
};
};
它说:“错误C3766:缺少插件接口::IPlugin::CreateInstance(…)的实现
我到底做错了什么
编辑:
忘了抽象陈述
还有:为什么说“IntelliSense:Class不能实现接口成员函数”Plugin\u interface::IPlugin::CreateInstance(在“Plugin\u interface.dll”中声明)
?您从这个片段中获得了更多的诊断信息,您犯了几个错误:
- [AttributeUsage]仅对从System::Attribute派生的类有效。毫无疑问,您需要使用某种属性,以便插件主机能够将您的类识别为有效的插件候选类,我猜不出该属性可能是什么
- 实现接口方法的方法应该是公共的
- 实现接口方法的方法必须是虚拟的
- 方法签名必须与接口方法声明完全匹配
- 以防万一:您必须实际实现该方法,而不仅仅是声明它
[NoIdeaWhatAttribute]
public ref class MyPlugin : public Plugin_Interface::IPlugin {
public:
virtual bool CreateInstance(SharedLibrary::MemoryArbiter^% pntMemory,
SharedLibrary::clsMessageQueue^% pntMessageQueue,
SharedLibrary::clsGPIO^% pntGPIO,
SharedLibrary::Types^% pntProgramSettings,
SharedLibrary::DisplayDriver^% pntDisplayDriver)
{
// Todo...
return false;
}
};
我明白了。多亏汉斯·帕桑给了我这么多提示:) 要导出函数,它必须实现接口1:1。必须在类头上添加export语句:
[Export(IPlugin::typeid)]
public ref class MyPlugin : public Plugin_Interface::IPlugin
而且:虽然VB.NET将编译为“任意CPU”,而C++/CLI将编译为Win64/Win32,但这两个项目将不匹配。两个项目必须具有相同的目标-64位或32位
现在它可以工作了。谢谢你的提示。现在我有了这样的功能:virtual bool CreateInstance(SharedLibrary::MemoryArbiter^,SharedLibrary::clsMessageQueue^,SharedLibrary::clsGPIO^,SharedLibrary::Types^,SharedLibrary::DisplayDriver^)=插件接口::IPlugin::CreateInstance;virtual bool DeleteInstance(void)sealed=Plugin\u Interface::IPlugin::DeleteInstance;关于DeleteInstance(等等)缺少实现的编译错误现在已经消失,但它仍然声明CreateInstance(在=)后面的部分)。这不是我建议你写的。使用显式接口方法实现是可以的,但实际上对插件没有任何意义。我看不出评论中有错误,但很难阅读,我看不到错误消息。当你请求帮助时,正确引用错误消息非常重要,你没有这样做在你最初的问题中也是这样。实际上这是一个插件,它应该使用MEF与主机应用程序进行通信。因此,我需要完全实现IPlugin接口。但是,我使用的是VS的德语版本,因此错误消息不会有多大帮助。哈利路亚,一个真正的错误消息。
^%
和^
不是一个可以忽略的微妙问题。也不是英语:)只需为System.ComponentModel.Composition.dll程序集添加程序集引用即可。如果通过谷歌搜索“c++/cli添加程序集引用”,请单击“提问”按钮“没有帮助。这不是问题的答案。那么你认为答案是什么?