Cannot apply operator '&' to r-value

Hello!
I’m new to C++ and UE. And I ran into a problem while trying to use SetTimer().
In my case:
GetWorld()->GetTimerManager().SetTimer(ReloadTimer, this, &ACannon::Reload(), 1/FireRate, false); IDE Keeps getting an Error: Cannot apply operator ‘&’ to r-value. The issue is here &ACannon::Reload() but I’m not sure how to ssolve it.

ACannon::Reload() means calling the function. You need to pass function pointer to SetTimer(…).

So new code should be :

GetWorld()->GetTimerManager().SetTimer(ReloadTimer, this, &ACannon::Reload, 1/FireRate, false);

2 Likes

The issue is that you’re passing a function call instead of a function pointer. Remove the parentheses after Reload:

GetWorld()->GetTimerManager().SetTimer(
    ReloadTimer,
    this,
    &ACannon::Reload,
    1.0f / FireRate,
    false
);

&ACannon::Reload is a pointer to the member function, whereas &ACannon::Reload() tries to call the function first, which causes the Cannot apply operator '&' to r-value error.

Also, use 1.0f / FireRate instead of 1 / FireRate to avoid integer division if FireRate is an integer.