本文介绍了C ++继承/覆盖基类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





通常,AFuelTank类继承自AActor。 AFuelTank包含方法

Hi,

Usually, the class AFuelTank inherits from AActor. AFuelTank contains the method

virtual void BeginPlay() override;

因为它能够覆盖AActor的BeginPlay()方法。



我现在在中间引入一个类,因为AFuelTank与其他AActor实现共享某些属性。我试图让AFuelTank从ASpawnable继承而来自AActor。



为了澄清,这里有一个结构的例子。



AFuelTank

AWaterTank - 全部继承自ASpawnable

APlayer



AOne

ATwo - 全部继承自ANumber

AThree



ASpawnable和ANumber然后继承自基类AActor,每个都有

as it is able to override AActor's BeginPlay() method.

I'm now introducing a class in the middle as AFuelTank shares certain properties with other AActor implementations. I'm trying to have AFuelTank inherit from ASpawnable, and that from AActor.

To clarify, here's an example of the structure going on here.

AFuelTank
AWaterTank - all inherit from ASpawnable
APlayer

AOne
ATwo - all inherit from ANumber
AThree

ASpawnable and ANumber then inherit from the base class AActor, and each have

virtual void Init()

,用于AFuelTank,AWaterTank等覆盖。



我是得到关于这种继承和覆盖的一系列错误。



以下是每种方法的使用方法:



当调用AFuelTank :: BeginPlay()时,它应该调用ASpawnable的BeginPlay(),这将调用AActor的BeginPlay();



当AFuelTank :: Init时()被调用,它应该调用AFuelTank的Init()的具体实现,因为虚拟的void Init()是p在ASpawnable中重新准备好被覆盖。



你能帮我找出每种方法的'虚拟'和'覆盖'的正确组合吗?



这是继承树 []



编辑:

for AFuelTank, AWaterTank etc to override.

I'm getting a whole host of errors regarding this inheritance and overriding.

Here's how each method should be used:

When AFuelTank::BeginPlay() is called, it should call ASpawnable's BeginPlay(), and that will call AActor's BeginPlay();

When AFuelTank::Init() is called, it should call AFuelTank's specific implementation of Init(), as a virtual void Init() is present in ASpawnable ready to be overridden.

Could you please help me figure out the correct combinations of 'virtual' and 'override' for each of these methods?

This is the inheritance tree here[^]


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ASpawnable.generated.h"

UCLASS()
class NOCTURNALSYNERGY_API AASpawnable : public AActor
{
	GENERATED_BODY()

public:
	//virtual void Init();

protected:
	//virtual void BeginPlay();

};
#include "ASpawnable.h"

//void AASpawnable::Init()
//{
//
//}
//
//void AASpawnable::BeginPlay()
//{
//	Super::BeginPlay();
//}
#pragma once

#include "../../Helpers/DebugHelper.h"
#include "../../Helpers/MathsHelper.h"
#include "ContainerAttributes.h"
#include "../ASpawnable.h"

#include "CoreMinimal.h"
#include "FuelTank.generated.h"

UCLASS()
class NOCTURNALSYNERGY_API AFuelTank : public ASpawnable
{
	GENERATED_BODY()

public:
	AFuelTank();
	//virtual void Init() override;

	// returns the tank's level
	int GetLevel();

	// returns the tank's capacity
	int GetCapacity();

	// returns the amount of fuel in the tank
	int GetFuel();

	// returns the tank's max health
	int GetMaxHealth();

	// returns the tank's health
	int GetHealth();

	// upgrades the tank to the next level
	void Upgrade();

	// decreases fuel amount if possible, returns amount removed
	int DepleteFuel(int amount);

	// increases fuel amount if possible, returns amount added
	int AddFuel(int amount);

protected:
	//virtual void BeginPlay() override;

private:
	ContainerAttributes containerAttributes;
	const int maxLevel = 4;

};
#include "FuelTank.h"

AFuelTank::AFuelTank()
{

}

//void AFuelTank::Init()
//{

//}

//void AFuelTank::BeginPlay()
//{
//	Super::BeginPlay();
//}

int AFuelTank::GetLevel()
{
	return containerAttributes.GetValue("level");
}

int AFuelTank::GetCapacity()
{
	return containerAttributes.GetValue("capacity");
}

int AFuelTank::GetFuel()
{
	return containerAttributes.GetValue("amount");
}

int AFuelTank::GetMaxHealth()
{
	return containerAttributes.GetValue("maxHealth");
}

int AFuelTank::GetHealth()
{
	return containerAttributes.GetValue("health");
}

void AFuelTank::Upgrade()
{
	containerAttributes.SetValue("level", GetLevel() + 1);
}

int AFuelTank::DepleteFuel(int amount)
{
	int fuel = GetFuel();
	int toRemove = MathsHelper::Min(amount, fuel);

	containerAttributes.SetValue("amount", fuel - toRemove);
	return toRemove;
}

int AFuelTank::AddFuel(int amount)
{
	int fuel = GetFuel();
	int capacity = GetCapacity();
	int toAdd = MathsHelper::Min(amount, capacity - fuel);

	containerAttributes.SetValue("amount", fuel + amount);
	return amount;
}





错误:



FuelTank.h:



Errors:

FuelTank.h:

class NOCTURNALSYNERGY_API AFuelTank : public ASpawnable



ASpawnable上的错误 - 不是类或结构名称



附加错误EXEC:无法在当前模块或目前解析的任何其他模块中找到名为'ASpawnable'的'FuelTank'的父类型。



我的尝试:



-------------------- -------------------------------------------------- ---------


Error on ASpawnable - "Not a class or struct name"

Additional error EXEC: "Couldn't find parent type for 'FuelTank' named 'ASpawnable' in current module or any other module parsed so far."

What I have tried:

-------------------------------------------------------------------------------

推荐答案

class NOCTURNALSYNERGY_API AASpawnable : public AActor

class NOCTURNALSYNERGY_API AFuelTank : public ASpawnable



简单的拼写错误。


Simple spelling error.


这篇关于C ++继承/覆盖基类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:53