Adding const to Fire() function produces error

Hello,
I’m using Rider i.o. VSC and sometimes it gives tips when to put a const keyword for variable or function. In this lecture we have added functionality for our tank to Fire. And Rider suggests to make void ABasePawn::Fire() const function. After I change it to const in BasePawn.h and BasePawn.cpp I get the following error in Tank.cpp. Any idea of the reason why?

Compilation log:

Using Visual Studio 2022 14.30.30709 toolchain (C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705) and Windows 10.0.19041.0 SDK (C:\Program Files (x86)\Windows Kits\10).
Building 4 actions with 24 processes...
  [1/4] Tank.cpp
   C:\repos\Unreal\ToonTanksV2\Source\ToonTanks\Tank.cpp(25) : error C2664: 'FInputActionBinding &UInputComponent::BindAction<ATank>(const FName,const EInputEvent,UserClass *,void (__cdecl ATank::* )(void))': cannot convert argument 4 from 'void (__cdecl ABasePawn::* )(void) const' to 'void (__cdecl ATank::* )(void)'
          with
          [
              UserClass=ATank
          ]
  C:\repos\Unreal\ToonTanksV2\Source\ToonTanks\Tank.cpp(25): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  C:\Program Files\Epic Games\UE_4.27\Engine\Source\Runtime\Engine\Classes\Components/InputComponent.h(867): note: see declaration of 'UInputComponent::BindAction'

const is part of the function signature and unfortunately Unreal doesn’t take that into account. The type deduced by Unreal to bind is

void (ATank::*)()

But you’re trying to give it

void (ATank::*)() const

Which is incompatible. Small demo:
Compiler Explorer

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms