【发布时间】:2022-06-03 05:55:27
【问题描述】:
我想用一个链接到稍后声明的Enemy 的参数创建方法。
这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
class Weapon{
public:
int atk_points;
string name;
string description;
void Attack(Entity target){
};
};
class Armor{
public:
int hp_points;
string name;
string description;
int block_chance;
};
class Entity{
public:
int hp;
int atk;
string name;
vector<Weapon> weapons;
vector<Armor> armors;
};
我试图寻找答案,但没有发现任何有用的信息。 这是错误日志:
prog.cpp:9:15: error: ‘Entity’ has not been declared
void Attack(Entity target){
【问题讨论】:
-
请注意,您应该通过引用获取参数(或者在某些情况下是指向它的指针) - 复制
target不太可能是您想要的 -
@UnholySheep 的好评论。我可以建议您阅读 Marc Gregoire 的“Professional C++”或任何其他可能更基础的 C++ 书籍 - 但这是最新的,即在过去两年中出版并涵盖 C++20。跨度>
-
您可以将
Entity替换为auto,即使这样的代码看起来很难看。
标签: c++ class header-files declaration