Header include problem

I am really desperate trying to figure this thing out for hours and hours.
Please if anybody can help, I would really appreciate.

I have 3 header files:

Component.h

1
2
3
4
5
6
7
8
9
10
#ifndef COMPONENT_H
#define COMPONENT_H
class Transform;

class Component
{
public:
      Transform *transform;
}
#endif 


Transform.h

1
2
3
4
5
6
7
8
9
#ifndef TRANSFORM_H
#define TRANSFORM_H

#include "Component.h"
class Transform : public Component
{
     //Has to inherit from Component
}
#endif 


SpriteRenderer.h

1
2
3
4
5
6
7
#pragma once
#include "Transform.h"

class SpriteRenderer : public Component
{
      //Some variables not refering to above classes
}


SpriteRenderer.cpp

Uses transform variable inherited from Component. Error here.
(And bunch of other linker errors)


Please help I`m really desperate.
Last edited on
Hi,

First up, if you have errors, then post them here verbatim - it makes things much easier.

There should be semicolons at the end of your class declarations - for example:

5
6
7
8
9
10
class Component
{
public:
      Transform *transform;
} ; // must put semicolon here


Edit:

If you use an IDE, you should be able to get it to write the bare bones of the class for you. That is, the header guards, the class declaration itself, and any member or function declarations. It should create both the header file and the .cpp file. There is an add class menu item somewhere in VS. I presume that is what you are using given that you have the #pragma once

/Edit

A question: Why does Component (a base class) own a pointer to Transform (a derived class)? Why do you do that when Component is inherited anyway?

Would it not be better for each of the classes constructor's initialiser list to call the the constructor of it's parent class? This way you are initialising everything, which then allows one to access the necessary functions from parent classes. Hopefully all your data is private, and you have accessor functions.


Hope all goes well
Last edited on
Hi TheIdeasMan, and thanks for reply! :D

Yes I had semicolon after class, I guess I forgot to add them to my post.

The Component class had pointer to transform, because I use this function which attaches any component(Like SpriteRenderer or Collider) to GameObject.

1
2
3
4
5
6
7
8
9
void GameObject::AddComponent(Component& newComponent)
{
	//If there isn`t already same component attached
	if (components.count(&typeid(newComponent)) == 0)
	{
		//Attach the component
		components[&typeid(newComponent)] = &newComponent;
	}
}


The component classes have to access transform component of gameObject to get position, scale etc. to function.

If I remove *transform pointer from Component class, the function above won`t work, since I don`t know how to pass transform to component class itself (Like SpriteRenderer or Collider) but only to Component parent class.

But I guess my approach is wrong and bad since I have these kinds of problems :) I am just trying to figure out some easy Component based system.
Hi,

My bad, firstly I edited my post about 10 times in quick succession.

My next bad, I am confused about the fact it's only a pointer to Transform, so pretty much all that I said was probably wrong. Creating a raw pointer to a type like that doesn't construct an object?

Despite all that - here is more confusion: Component "HAS A" (pointer to) Transform, but Transform "IS A" Component - that sounds fishy :+)

It was my understanding that a class owns pointers to components, (Physics, Audio etc). But you have then included a component in the inheritance. Can you not just keep Transform out of the inheritance then initialise the pointer to Transform in the constructor?

Never mind, I think I am being more of a hindrance than help right now, I haven't given any concrete solutions.
hehe no problem, you gave me some ideas to think about :D

The thing I need is, the SpriteRenderer component needs position information from Transform component where it should draw Sprite/Texture on screen :D The some goes for collision etc.

In my current implementation, when I "attach" this kind of component to GameObject class, i don`t know how to pass "transform/position" information from Transform component attached to GameObject(every object has to have one transform component) to SpriteRenderer class. I can only pass to Component class since that is more of an Generic type.

(Sorry this is getting long)
Example:

I want my enemy to have SpriteRenderer since I want to see him drawn on screen. I write:

AddComponent(SpriteRenderer);

This component is now attached to GameObject(my enemy). But now when enemy moves, SpriteRenderer needs position information from transform to update drawing.

But I cannot manage to pass it somehow.


EDIT: Okay it seems to compile, only as you said, Transform does not inherit from Component. Which doesn`t makes much sense in Entity Component design, but for the sake I don`t lose my mind and you time here I let it be like this :D

Thanks for the help xD
Last edited on
Topic archived. No new replies allowed.