I'm currently working on building a small ECS system, and in the process, I created an Application.h and Gameobject.h, where the Gameobject.h file requires both the declaration of the application class and some of its methods, and Application.h requires the gameobject class and some of its methods. I tried using a forward declaration of both the classes, but that doesn't help when it comes to getting class methods that are yet undefined, as the forward declaration doesn't define the class methods.
This is Gameobject.h:
#pragma once
#include "Common.h"
class Gameobject
{
friend class Application;
public:
Gameobject(std::string name)
:name(name)
{
Application::Get()->AddGameobject(this);
}
std::string name;
template<typename TComponent>
void AddComponent()
{
DERIVES_FROM_COMPONENT;
TComponent* component = new TComponent();
component->SetGameobject(this);
m_Components.push_back((Component*)&component);
}
template<typename TComponent>
void RemoveComponent()
{
for (const Component& component : m_Components)
{
if (typeid(component) == TComponent)
{
m_Components.erase(component);
delete component;
return;
}
}
}
private:
void OnUpdate()
{
for (Component* component : m_Components)
{
component->OnUpdate();
}
}
std::vector<Component*> m_Components;
};
And Appplication.h:
#pragma once
#include "Common.h"
class Application
{
friend class Gameobject;
public:
static Application* Get()
{
//make a unique pointer later
static Application* s_Instance = new Application();
return s_Instance;
}
void Run()
{
std::cout << "Running";
while (isRunning)
{
for (Gameobject* gameobject : m_Gameobjects)
{
gameobject->OnUpdate();
}
}
}
bool Close()
{
return true;
}
bool isRunning = true;
private:
Application() {};
~Application() { delete Get(); }
void AddGameobject(Gameobject* gameobject)
{
m_Gameobjects.push_back(gameobject);
}
void RemoveGameobject(Gameobject* gameobject)
{
const auto& it = std::find(m_Gameobjects.begin(), m_Gameobjects.end(), gameobject);
m_Gameobjects.erase(it);
}
std::vector<Gameobject*> m_Gameobjects;
};
then Common.h:
#pragma once
#include <iostream>
#include <vector>
class Gameobject;
#include "ECS.h"
#include "Components.h"
#include "Application.h"
#include "Gameobject.h"
So the gameobject constructor requires the application.AddGameobject method and Application.Run() require gameobject.OnUpdate()
I'm still relatively new to C++, so is there something I'm missing that would make linking and compiling this easier or otherwise just solve the issue?
-thanks