VC++ 2005 B2 error C3766:'ClassXXX' must provide an implementation for the interface method '

Hi All:

I have complied the code from MSDN library for Vs2005b2, which is the example of 'interface class'. The codes are as follows:

//=========================================
// mcppv2_interface_class.cpp
// compile with: /clr
using namespace System;

public delegate void ClickEventHandler(int, double);

// define interface with nested interface
public interface class Interface_A {
void Function_1();

interface class Interface_Nested_A {
void Function_2();
};
};

// interface with a base interface
public interface class Interface_B : Interface_A {
property int Property_Block;
event ClickEventHandler^ OnClick;
static void Function_3() { Console::WriteLine("in Function_3"); }
};

// implement nested interface
public ref class MyClass : public Interface_A::Interface_Nested_A {
public:
void Function_2() { Console::WriteLine("in Function_2"); }
};

// implement interface and base interface
public ref class MyClass2 : public Interface_B {
private:
int MyInt;

public:
// implement non-static function
void Function_1() { Console::WriteLine("in Function_1"); }

// implement property
property int Property_Block {
int get() { return MyInt; }
void set(int value) { MyInt = value; }
}
// implement event
event ClickEventHandler^ OnClick;

void FireEvents() {
OnClick(7, 3.14159);
}
};

// class that defines method called when event occurs
ref class EventReceiver {
public:
void OnMyClick(int i, double d) {
Console::WriteLine("OnClick: {0}, {1}", i, d);
}
};

int main() {
// call static function in an interface
Interface_B::Function_3();

// instantiate class that implements nested interface
MyClass ^ x = gcnew MyClass;
x->Function_2();

// instantiate class that implements interface with base interface
MyClass2 ^ y = gcnew MyClass2;
y->Function_1();
y->Property_Block = 8;
Console::WriteLine(y->Property_Block);

EventReceiver^ MyEventReceiver = gcnew EventReceiver();

// hook handler to event
y->OnClick += gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

// invoke events
y->FireEvents();

// unhook handler to event
y->OnClick -= gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

// call implemented function via interface handle
Interface_A^ hi = gcnew MyClass2();
hi->Function_1();
}

//=========================================

But I got many errors, most of which like :


error C3766: 'MyClass' must provide an implementation for the interface method 'void Interface_A::Interface_Nested_A::Function_2(void)'


I don't see any syntax error in the code, and I can't find the explanation for 'error C3766' in MSDN document. My VS2005 version is 8.0.50215.44 (beta2.050215-4400).

Is there anybody can give me some advice? Thanks

[3754 byte] By [zorleo] at [2007-12-16]
# 1

Implementation of interface-methods requires the "virtual" keyword!

So just add to every interfecae method/property/event the word "virtual" and everything is working:

//=========================================
// mcppv2_interface_class.cpp
// compile with: /clr
using namespace System;

public delegate void ClickEventHandler(int, double);

// define interface with nested interface
public interface class Interface_A {
void Function_1();

interface class Interface_Nested_A {
void Function_2();
};
};

// interface with a base interface
public interface class Interface_B : Interface_A {
property int Property_Block;
event ClickEventHandler^ OnClick;
static void Function_3() { Console::WriteLine("in Function_3"); }
};

// implement nested interface
public ref class MyClass : public Interface_A::Interface_Nested_A {
public:
virtual void Function_2() { Console::WriteLine("in Function_2"); }
};

// implement interface and base interface
public ref class MyClass2 : public Interface_B {
private:
int MyInt;

public:
// implement non-static function
virtual void Function_1() { Console::WriteLine("in Function_1"); }

// implement property
property int Property_Block {
virtual int get() { return MyInt; }
virtual void set(int value) { MyInt = value; }
}
// implement event
virtual event ClickEventHandler^ OnClick;

void FireEvents() {
OnClick(7, 3.14159);
}
};

// class that defines method called when event occurs
ref class EventReceiver {
public:
void OnMyClick(int i, double d) {
Console::WriteLine("OnClick: {0}, {1}", i, d);
}
};

int main() {
// call static function in an interface
Interface_B::Function_3();

// instantiate class that implements nested interface
MyClass ^ x = gcnew MyClass;
x->Function_2();

// instantiate class that implements interface with base interface
MyClass2 ^ y = gcnew MyClass2;
y->Function_1();
y->Property_Block = 8;
Console::WriteLine(y->Property_Block);

EventReceiver^ MyEventReceiver = gcnew EventReceiver();

// hook handler to event
y->OnClick += gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

// invoke events
y->FireEvents();

// unhook handler to event
y->OnClick -= gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

// call implemented function via interface handle
Interface_A^ hi = gcnew MyClass2();
hi->Function_1();
}

//=========================================

Greetings
Jochen

JochenKalmbach at 2007-8-21 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
Hello Jochen Kalmbach.
Thanks for your response, I have resolved the problem in your way. It seems the MSDN example and error explanation need to be updated.
zorleo at 2007-8-21 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
See also:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=b3565bd5-2435-4fa1-a47b-a5db43f2e930

And please mark this thread as answered!

Greetings
Jochen

JochenKalmbach at 2007-8-21 > top of Msdn Tech,Visual C++,Visual C++ General...