views:

575

answers:

2

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.

+1  A: 

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

public ref class B : public A { void M( unsigned short u ); }
Stu Mackellar
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.
You shouldn't have the public keyword on the interface declaration in C#.
Stu Mackellar
If I remove the public keyword it doesn't recognize the interface in C++.
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
Stu Mackellar
+1  A: 

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.

Hans Passant