Search code examples
c++interfacemanaged-c++

How to implement an interface in managed c++?


Here's interface that I have declared:

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

Here's the class that is trying to implement it:

ref class MyShedluer:IShedluer
{
    Shedluer ^shedluer;//this is NOT MyShedluer
public:
    MyShedluer(void);

    array<Object^>^ GetResult(UInt64 taskId)
    {
        return shedluer->GetResult(taskId);
    }
}

When I'm trying to compile this, I'm getting

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

Why am I getting this?


Solution

  • the correct syntax for implementing an interface is to add virtual:

    ref class MyShedluer:IShedluer
    {
    public:
      virtual array<Object^>^ GetResult(UInt64 taskId);
    }
    

    Also the compiler tells you this, look at your warnings as well:

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