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

时间:2012-01-19 12:05:56

标签: c++ interface managed-c++

这是我声明的界面:

[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:\users\menkaur\documents\visual studio 2010\projects\MyProject\
\kernel\MyShedluer.h    78  1   MyProject.Kernel

为什么我会这样做?

1 个答案:

答案 0 :(得分:5)

实现接口的正确语法是添加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'