Tuesday, April 5, 2011

C++ Header implementing a C# Interface

Hi.

Basically I want to know if it is possible to implement a C++ header from a C# interface, because I can't make it work.

Example:

C# Interface: public interface A { void M( ushort u ); }

C++ header: public ref class B : A { void M( unsigned short u ); }

Returns "Error C3766".

Thanks in advance.

From stackoverflow
  • The interface must also be public. Try this instead (note 2nd public keyword):

    public ref class B : public A { void M( unsigned short u ); }
    
    : Thanks for replying. I tried: C# Interface: public interface A { void M( ushort u ); } C++ header: public ref class B : public A { void M( unsigned short u ); } and it didn't work.
    Stu Mackellar : You shouldn't have the public keyword on the interface declaration in C#.
    : If I remove the public keyword it doesn't recognize the interface in C++.
    Stu Mackellar : Sorry, I misread your comment. C3766 implies that the interface implementation is missing, so the only other thing I can suggest is to explicitly set M's parameter type to UInt16 in C# and C++. ushort should map fine to unsigned short though.
    Stu Mackellar : Ignore the rest of this. Nobugz is right - you're missing the virtual keyword on the interface implementation. +1 to nobugz
  • You have to use the virtual keyword. This compiled as expected:

    public ref class B : ClassLibrary1::A {
    public:
        virtual void M(unsigned short u) {}
    };
    

    Where ClassLibrary1 was the namespace in which the C# interface declaration was made.

0 comments:

Post a Comment