Original Post
I seem to run into this issue where I try to use seperate file compilation and I end up getting the message:
error: ‘Component’ was not declared in this scope[/quote]
I've declared the Component file, but I think this is a cyclic issue of some sort. Is there a way that I can avoid this problem?
Here are the files:
[source lang="cpp"]// main.cpp
#include
#include
#include "gameobject.h"
#include "component.h"
int main ()
{
GameObject* go = new GameObject("Player");
Component* gun = new Component;
go->AddComponent(gun);
}
[/source]
[source lang="cpp"]#ifndef COMPONENT_H
#define COMPONENT_H
#include
#include
#include "gameobject.h"
class Component
{
public:
std::string name; //The name of the component
bool enabled; //Is the component currently enabled?
GameObject* gameObject; //The game object that owns this component
int type; //The type of component
//Initializing Ctor
Component(std::string Name) : name(Name), enabled(true) {}
//Dtor
virtual ~Component() {}
//Abstract function, called when the scene is started
virtual void OnStart() = 0;
//Abstract function, called when the scene is updated
virtual void OnUpdate() = 0;
};
#endif // COMPONENT_H
[/source]
[source lang="cpp"]#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
#include
#include
#include
#include "component.h"
class GameObject
{
// ERROR STARTS AT CODE BELOW (notice 'component.h' is declared above):
std::vectorcomponents; //A list of components attached to the game object
public:
std::string name; //The unique name of the game object
//Transform transform; //The object's transform
//Initializing Ctor
GameObject(std::string Name = "Default Player") : name(Name) { }
~GameObject() { }
//Finds the attached component with the name Name (if it exists)
/* Component* GetComponent(std::string Name)
{
}
//Finds the attached component with the index Idx (if it exists)
Component* GetComponentAt(int Idx)
{
}*/
//Attaches a component to the game object
void AddComponent(Component* NewComponent)
{
components.push_back(*NewComponent);
}
//Detaches the component with the name Name (if it exists)
void RemoveComponent(std::string Name)
{
}
//Detaches the component with the index Idx (if it exists)
void RemoveComponentAt(int Idx)
{
}
//Initializes the game object by initializing each of its attached components
void Start() {}
//Updates the game object by updating each of its attached components
void Update() {}
};
#endif // GAMEOBJECT_H
[/source]