如何在托管c++中实现接口

How to implement an interface in managed c++?

本文关键字:实现 接口 c++      更新时间:2023-10-16

下面是我声明的接口:

[ServiceContract]
public interface class IShedluer
{
    [OperationContract]
    array<Object^>^ GetResult(UInt64 taskId);
}

下面是试图实现它的类:

ref class MyShedluer:IShedluer
{
    Shedluer ^shedluer;//this is NOT MyShedluer
public:
    MyShedluer(void);
    array<Object^>^ GetResult(UInt64 taskId)
    {
        return shedluer->GetResult(taskId);
    }
}

当我试图编译这个时,我得到了

Error   15  error C3766: 'MyShedluer' must provide an implementation for 
the interface method 'cli::array<Type> ^IShedluer::GetResult(unsigned __int64)'
d:usersmenkaurdocumentsvisual studio 2010projectsMyProject
kernelMyShedluer.h    78  1   MyProject.Kernel

为什么我会得到这个?

实现接口的正确语法是添加virtual:
ref class MyShedluer:IShedluer
{
public:
  virtual array<Object^>^ GetResult(UInt64 taskId);
}

编译器也告诉你这一点,看看你的警告:

warning C4488: 'MyShedluer::GetResult' : requires 'virtual' keyword
to implement the interface method 'IShedluer::GetResult'