SetOwner in C++

I’m trying to call SetOwner() in my weapon class.

Here is the code:

void ABaseWeapon::SetOwningPawn(ABaseCharacter* NewOwner)
{
/if (MyPawn != NewOwner)
{
Instigator = NewOwner;
MyPawn = NewOwner;
// net owner for RPC calls
//SetOwner(NewOwner);
}
/
}

I can’t understand why I have this error: "Can’t convert ABaseCharacter in APawn

Any idea anyone ?

Make sure you have included the .h file for your ABaseCharacter class in the .cpp file of your ABaseWeapon class. The compiler needs to know that you’re assigning a pointer of a type that converts to APawn/AActor.

Thanx for your response but it doesn’t work because if I do so, there is a circular dependancy .

So I made this declaration :
class ABaseCharacter;

I’m trying to do the same that in shooter example but for me it doesn’t work.

There shouldn’t be a circular dependency by including that header in your .cpp, as long as your headers have #pragma once at the top.

As I say, the compiler needs to know the complete type of ABaseCharacter in order to do the pointer assignment - a forward declaration won’t suffice.

I tryed but the compilator say there is a circular dependancy.

In my weapon class, there is a variable MyPawn (class Base Character)
and in my class BaseCharacter there is some variable weapon.

That’s why if I include BaseCharacter in the header of my weapon class there is a circular dependancy.

Sure, if each of the headers has classes containing members of each other, then that is impossible. And unless you have forward declarations then things can still break. In general having forward declarations in headers instead of includes is best practice (where possible).

Your setup should be something like this, Redemp:

BaseCharacter.h:

#pragma once

#include "GameFramework/Character.h"

class ABaseWeapon;

UCLASS()
class ABaseCharacter : public ACharacter
{
	GENERATED_BODY()

public:

	UPROPERTY()
	ABaseWeapon* MyWeapon;

};

BaseWeapon.h:

#pragma once

#include "GameFramework/Actor.h"

class ABaseCharacter;

UCLASS()
class ABaseWeapon : public AActor
{
	GENERATED_BODY()

public:

	void SetOwningPawn(ABaseCharacter* NewOwner);

	ABaseCharacter* MyPawn = nullptr;

};

BaseWeapon.cpp:

#include "BaseWeapon.h"
#include "BaseCharacter.h"

void ABaseWeapon::SetOwningPawn(ABaseCharacter* NewOwner)
{
	if (MyPawn != NewOwner)
	{
		Instigator	= NewOwner;
		MyPawn		= NewOwner;
		// net owner for RPC calls
		SetOwner(NewOwner);
	}
}

It is possible to have circular dependencies even if use #pragma once.

I have seen that once before, which was introduced by 4 classes (Character-MethodPicker-Method-Object) when attempting using Strategy Pattern. I solved in converting all unnecessary include in headers files to only forward-declare on the global scope and added all needed in .cpp files.

If that possible to provide the source of your project to pinpoint where the includes go crazy?

Completely forgot to answer you, just realize that today but yeah, it did works.
Thank you so much for your help !!!