Dollarslice
Dollarslice

Reputation: 10314

#include confusion

in order to avoid an include circle, I have replaced an #include that was previously in a classes header into its cpp file. I then placed a forward declaration into the header.

This solved my problem, however, the source file no longer seems to be able to access the members of the included file in the cpp file. Why is this? and what is the solution?

EDIT - real code below:

header:

#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

source:

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

The first error is GameObject: base class undefined.

all the subsequent errors are "Error: Identifier "whatever" is undefined. They are all getters that are public and in gameobject.

Upvotes: 2

Views: 676

Answers (3)

Luchian Grigore
Luchian Grigore

Reputation: 258648

You must be getting a compiler error beforehand, forward declaration does not allow you to inherit from a class. Please post your code.

The following line:

class Bullet : public GameObject

means you have to include GameObject.h in Bullet.h. Again, a forward declaration is not sufficient for inheritance. If you get more errors, post the exact error code, text and line number (after you make the changes).

Upvotes: 6

phhkafot
phhkafot

Reputation: 183

class blah; 

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

What do you mean by "used to work but now doesn't"? Which error message are you getting?

Upvotes: 0

thiton
thiton

Reputation: 36059

The semicolon in the line defining class foo is superfluous:

class foo : public blah;

must read

class foo : public blah

Upvotes: 0

Related Questions