The object has type qualifiers that are not compatible

hello,in BattleTank game i’m having problem with this line of code:
GetControlledTank()->AimAt(GetPlayerTank()->GetActorLocation());
i have an error on GetControlledTank() that says:
the object has type qualifiers that are not compatible with the member function ATank::AimAt().

i tried googling it and finally found out that const objects can only invoke const member functions and then i tried to declare AimAt() method definition as const,but it’s still no use and i still get the same error.only when i remove const keyword in GetControlledTank() definition the code compiles with no error.anyway, i haven’t seen Ben having such an issue when he was implementing this.any idea what the problem is?
Thanks in advance!

Could you provide more information.
Where are you calling this function? What were your declarations with const?

thanks for your response.i have a c++ class called ATank which is a pawn. here is the declaration in Tank.h:
public:
const void AimAt(FVector HitLocation);
and in .cpp file i implemented it like this:
const void ATank::AimAt(FVector HitLocation){
auto TankName=GetName();
UE_LOG(LogTemp, Warning, TEXT("%s is at%s),*TankName,HitLocation.ToString());
}
and there is another c++ class called TankPlayerController at which i have GetControllledTank function here is the header file and .cpp:
public:
const ATank
GetControlledTank();

.cpp file:
cont ATank* GetControlledTank(){
cast(ATank)(GetPawn()); //Atank is actually in <> it wouldn’t show it if i wrote it in <> that’s why i used simple parentheses.
}
and i’m calling in tick function of TankPlayerController like so:
void ATankPlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
GetControlledTank()->AimAt(GetPlayerTank()->GetActorLocation());
}
i hope these are sufficient.

You have const on the wrong side

const ATank* GetControlledTank();

This means you’re returning a const actor that is const not a const member function to do that you would write

ATank* GetControlledTank() const;
1 Like

thanks its working.but i dunno the use of second one i mean how it works can you explain a little more ? i used to think whole my life that it doesn’t matter where you put the const keyword and both are the same.

As in the value that is returned is const.

const AActor* Actor = /*something*/;
//vs
AActor* Actor = /*something*/;
1 Like

look in this function: const ATank* GetControlledTank();
it means the value that it returns is const and cannot be changed right ? and you are saying the second one is also the same. then it means there is no difference. but u said they are different.

They are different.

const ATank* GetControlledTank();

This returns a const object. It is not a const member function.

ATank* GetControlledTank() const;

This on the other hand returns a non-const object but is a const member function.


A const member function means it can’t modify data members.

class Example
{
    int Value = 10;
    void Foo() const
    {
        Value = 2; //ERROR trying to modify data member in const member function
    }
};
1 Like

oh i see thanks a lot so if its just a void function like AimAt() can we use const on both sides ? i mean are these two the same:
1_const AimAt()
2_AimAt() const

No that’s the same thing as above although missing the type. const at the end and only the end of a function means it’s a const member function.

then something like this :const void AimAt() is meaningless right ? but i declared it like this and got no errors.although i still would get the error as you said cuz it wouldn’t recognize it as a const member function.it means the compiler just ignores the word const here right ?
and the last thing.sorry that i’m asking a lot.does it make sense to declare a normal function as const too ? i mean a function outside a class.thanks so much!

Yes.

There’s no such thing as a const non-member function - what would the meaning of that be? - so that would fail to compile.

1 Like

thank you for your perfect and exact answers!.:pray:

1 Like

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

Privacy & Terms