C++,#include 问题!

标签 c++

我的包含在名为“stats.h”的 header 中

我有 2 个类 AB

A需要B的头部,A需要B

的头部

当我尝试编译时,我遇到了大约 50 个这样的错误

identifier 'Player'
int assumed
; before * missing

关于如何解决这个问题有什么想法吗? :(

(这里是相关代码)

A 级 - 玩家

#include "stats.h"

class Player
{
public:
Player(int x, int y);

Texture *texture;

int x;
int y;

float rotation;

void Update(double elapsedTime);
void Draw(Sprite *sprite);

private:
Weapon *weapon;
};

B 级 - 武器

#include "stats.h"

class Weapon // **CLASS B**
{
public:
double interval;

Weapon(Player *player);

void Update(double elapsedTime);
void Draw(Sprite *sprite);

protected:

private:
Player *player;
};

STATS.H

#include "Player.hpp"
#include "Weapon.hpp"

最佳答案

您的 header 中没有实现,因此您可以传递前向声明并仅在 cpp 文件中包含 header 。


例如-在class Player之前,提出class Weapon的声明:

//vvvvvvvvvvv
class Weapon;

class Player
{
public:
    Player(int x, int y);
    /* .. */
};

并删除 #include "stats.h"class Weapon

相同

关于C++,#include 问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5894385/

相关文章:

c++ - wglCreateContextAttribsARB 在 x64 平台下的 Debug模式下崩溃

c++ - 这个 valgrind 错误是什么意思?

运行时的 C++ 变量类型选择

c++ - 编译 C++ 程序的 Makefile 问题

c++ - 如何使用 ULARGE_INTEGER 进行算术运算

c++ - 在 Unix/Linux 中将 Windows 文件时间转换为秒

c++ - 加载位图文件 (.bmp)

c++ - 错误 : passing ‘const Vector<int>’ as ‘this’ argument of . .. 丢弃限定符 [-fpermissive]

c++ - Silverlight for Windows Embedded 7 中的多页应用程序

c++ - 如何将模板可变参数存储到 std::ofstream 中?