#include混乱

时间:2011-11-08 12:26:26

标签: 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

源:

#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;
}

第一个错误是GameObject:基类未定义。

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

3 个答案:

答案 0 :(得分:6)

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

以下一行:

class Bullet : public GameObject

表示您必须在GameObject.h中加入Bullet.h。同样,前向声明不足以进行继承。如果您收到更多错误,请发布确切的错误代码,文本和行号(在您进行更改后)。

答案 1 :(得分:0)

定义类foo的行中的分号是多余的:

class foo : public blah;

必须阅读

class foo : public blah

答案 2 :(得分:0)

class blah; 

class foo : public blah // <- extra ; here
{ 
    public foo(); 
}; 

你的意思是“曾经工作,但现在没有”?您收到了哪条错误消息?

相关问题