Compiler Error C3731

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Compiler Error C3731.

incompatible event 'function1' and handler 'function2'; event source and event handler must be the same type

The event source and event receiver must have the same type (for example native vs. com types). To fix this error, make the types of the event source and the event handler match.

The following sample generates C3731:

// C3731.cpp  
// compile with: /clr  
#using <mscorlib.dll>  
[event_source(native)]  
struct A {  
   __event void MyEvent();  
};  
  
[event_receiver(managed)]  
// try the following line instead  
// [event_receiver(native)]  
struct B {  
   void func();  
   B(A a) {  
      __hook(&A::MyEvent, &a, &B::func);   // C3731  
   }  
};  
  
int main() {  
}  

Show: