Incomplete type is not allowed

Hello!

Why am I getting these errors?


Error	1	error C2079: 'Main::m_Graphics' uses undefined class 'Graphics'
Error	2	error C2079: 'Main::m_Events' uses undefined class 'Events'


...

	8	IntelliSense: incomplete type is not allowed
	9	IntelliSense: incomplete type is not allowed



This is Main.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

#include "Includes.h"

class Graphics;
class Events;

class Main
{
public:
	Main();
	~Main();

	void Start();

private:
	Graphics m_Graphics;
	Events m_Events;

};




this is "Includes.h"
1
2
3
4
5
6
7
8
9
#pragma once
#include "Definitions.h"
#include <cstdlib>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include <vector>

using std::vector;




this is "Definitions.h"
1
2
3
4
5
#pragma once

const int Screen_Width = 800;
const int Screen_Height = 600;
First in you Main.h your classes Graphics and Events are not define within a scope , or it is not defined properly.

1
2
3
4
5
6
7
8
class Graphics
{
    //TO DO: Put some code here
};
class Events
{
    //TO DO: Put some code here
};
What do you mean by not defined within a scope? How would I do that?

If I did this

1
2
3
4
5
6
7
8
class Graphics
{
    //TO DO: Put some code here
};
class Events
{
    //TO DO: Put some code here
};


Wouldn't it prevent me from having the class definition elsewhere, in another file?

I would just like to be able to give Main two objects: Graphics and Events, each of which having a pointer to Main. Is this what's called circular dependence?

While writing this post I managed to fix the issue by including the Graphics.h and Events.h files in Main.h, each of which is like this

1
2
3
4
5
6
7
8
9
10

class Main;

class Events or Graphics
{

//stuff

}


Why did using a forward declaration work for those classes but for Main I had to include the classes headers?
Your Main class wants to hold Graphics and Events objects. That means your compiler has to know the size of those objects, which means knowing the definition beforehand. But only the prototypes are provided, so the compiler cannot determine the size of this undefined classes.

Forward declaration of classes only works for pointers and references, because they don't need to know the size of an object. That's why for your Main class, you need to include the proper headers and why for your other two classes, forward declaration works.

However, if you are having two or more classes that have each other as member objects (whether or not through pointers), I would be suspect of the design. Why do Events and Graphics need pointers to Main?
Oh, got it, thanks!

I still am very much a beginner, so I am still learning the best programming practices. Therefore, I am not sure my design is good, but I would like to try it just to see how it goes. I am still reading a lot and just started getting into the intermediate stuff and this is the most elegant I have managed to make it. Before this, the functions that handle events and graphics and the control of the program were all outside of classes and in the same file. I am improving little by little!

They need pointers because I was thinking main would hold a vectors of Objects (menus, windows and other things), so Graphics needs to access main to get those objects in order to draw them, while Events needs a pointer to main to get to Graphics or Objects in order to respond to input, for example. This program is intended to be the framework of my future programs, by the way.

Also, there is something that may be preventing me from achieving the best design: I am very much of an OCD person when it comes to programming. For example, maybe giving those vectors of objects to Graphics would make the design better, but it really annoys me when a class has something that in my mind doesn't belong there. Luckily, I program exclusively as a hobby, so that wouldn't get in the way of my career or anything.
Last edited on
Topic archived. No new replies allowed.