Download

Declaring delegate which accepts containing class type

Edit: tried for hours and hours, gave up.

I want MyGameGameMode.cpp to pass a few functions by reference to TCPSocket.cpp, which should call the functions, using its own instance as a parameter for one of them, because one of the functions requires the TCPSocket instance.

In TCPSocket.h, just below the #includes, I have:


DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FNetMsgEventStr, UTCPSocket*, socket_impl, const FString&, msg);

Within the class, I have this under public:



template<typename obj>
void set_handlers(obj* o, void(*on_connected_ptr)(), void(*on_message_received_ptr)(UTCPSocket*, const FString&))
{
    on_connected.AddDynamic(*o, on_connected_ptr);
    on_message_received.AddDynamic(*o, on_message_received_ptr);
}


In MyGameGameMode.cpp, I call this with:


socket->set_handlers<AMyGameGameMode>(this, &AMyGameGameMode::on_connected, &AMyGameGameMode::on_message_received);

And that refers to these two static methods:



void AMyGameGameMode::on_connected()
{
    UE_LOG(LogTemp, Warning, TEXT("Connected to server!"));
}

void AMyGameGameMode::on_message_received(UTCPSocket* socket_impl, const FString& msg)
{
    UE_LOG(LogTemp, Warning, TEXT("Received message: %s"), *msg);
    socket_impl->send(TEXT("Thanks for the message, server!"));
}


This setup results in this error:


error C2672: 'TBaseDynamicMulticastDelegate<FWeakObjectPtr,void>::__Internal_AddDynamic': no matching overloaded function found
note: see reference to function template instantiation 'void UTCPSocket::set_handlers<AMyGameGameMode>(obj *,void (__cdecl *)(void),void (__cdecl *)(UTCPSocket *,const FString &))' being compiled with  obj =
AMyGameGameMode ]

Everything worked fine until I migrated from OneParam to TwoParams and added TCPSocket* to the method signatures and added socket_impl->send, so it’s purely the TCPSocket* part of the method signatures causing the issue.

Any ideas how to resolve this?

Side-note - how can I turn off automatic conversion to emojis on this forum?

What type/class is “UTCPSocket”? Based on your other thread, it doesn’t look as though it’s a UObject type. Dynamic Delegates are serializable, so they can’t be used with non-reflected types.

You probably want a non-dynamic delegate (which you can declare by just omitting the DYNAMIC section of the macro, and removing the parameter names).