#include混乱

Sir*_*lot 2 c++

为了避免使用包含圈,我将以前在类头中的#include替换为其cpp文件.然后我在标题中放置了一个前向声明.

这解决了我的问题,但是,源文件似乎不再能够访问cpp文件中包含文件的成员.为什么是这样?什么是解决方案?

编辑 - 下面的真实代码:

标题:

#ifndef BULLET_H
#define BULLET_H

#include "BoundingSphere.h"
#include "helperMethods.h"

class GameObject;

class Bullet : public GameObject
{
private:
float velocity;
float distance;
D3DXVECTOR3 firedFrom;
BoundingSphere bSphere;

public:
Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR     colour);
BoundingSphere BulletSphere();//change this to simply use position
void Update();
};

#endif
Run Code Online (Sandbox Code Playgroud)

资源:

#include "Bullet.h"
#include "GameObject.h"

Bullet::Bullet(D3DXVECTOR3 position, D3DXVECTOR3 rotation, float velocity, D3DXCOLOR colour) 
: bSphere(BoundingSphere(position, 0.01f))
{
//Model = content.Load<Model>("Models\\basicMesh");
//Texture = content.Load<Texture2D>("Textures\\basicMesh");

distance = 0.0f;

Scale(D3DXVECTOR3(0.25f, 0.1f, 0.1f));// error

Colour(colour); //error

firedFrom = position;

Rotation() = rotation; // error
this->velocity = velocity;
}

BoundingSphere Bullet::BulletSphere()
{
return bSphere;
}

void Bullet::Update()
{
Position(D3DXVECTOR3Helper.PointOnXYCircle(firedFrom, distance, Rotation().z)); //error
bSphere.Centre(Position());
distance += velocity;
}
Run Code Online (Sandbox Code Playgroud)

第一个错误是GameObject:基类undefined.

所有后续错误都是"错误:标识符",无论"未定义".它们都是公共的和游戏对象的getter.

Luc*_*ore 6

您必须事先获得编译器错误,前向声明不允许您从类继承.请发布您的代码.

以下行:

class Bullet : public GameObject
Run Code Online (Sandbox Code Playgroud)

意味着你必须包含GameObject.hBullet.h.同样,前向声明不足以进行继承.如果您收到更多错误,请发布确切的错误代码,文本和行号(在您进行更改后).