Compiler doesn't know what this class is

Hey guys,

I get an error saying the class "LocationHandler" is undefined, even when I forward declare it. It is of course located in a separate header file. The thing is, I also have a class called "Player" in another separate header file and I never even had to forward declare it for it to work, so this confuses me.

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <array>
#include <list>
#include <vector>
#include <algorithm>
#include <math.h>
#include "Entity.h"
#include "Game.h"
#include "Items.h"
#include "Location.h"

using namespace std;


int main(){

   sf::RenderWindow * pWindow = new sf::RenderWindow;
   pWindow->create(sf::VideoMode(1200, 800), "SINDER");

   sf::Font comFont;
   if (!comFont.loadFromFile("windows_command_prompt.ttf"))
   cout << "Couldn't Load windows_command_prompt.ttf" << endl;

   ScreenHandler * pScreenHandler = new ScreenHandler;

   Player * pPlayer = new Player;
   LocationHandler * pLocationHandler = new LocationHandler;

   MenuInputHandler * pMenuInput = new MenuInputHandler(pScreenHandler, pWindow, pPlayer);

   ActionInputHandler * pActionInput = new ActionInputHandler(pPlayer, pScreenHandler);

   BigScreenRegion * pBigScreenRegion = new BigScreenRegion(pScreenHandler, comFont, pWindow, pMenuInput, pActionInput, pLocationHandler)


///---Then I have input/update/render stuff // I don't think its relevant

}



Game.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <array>
#include <list>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cassert>


using namespace std;


class LocationHandler;


class ScreenRegion{
protected:
	ScreenHandler * m_pScreenHandler;
	sf::RenderWindow * m_pWindow;
	sf::Font m_Font;
	MenuInputHandler * m_pMenuInput;
	ActionInputHandler * m_pActionInput;
	vector<sf::Text> m_CurrentConfig;

public:

	ScreenRegion(ScreenHandler * pScreenHandler, sf::Font& font, sf::RenderWindow * pWindow, MenuInputHandler * pMenuInput, ActionInputHandler * pActionInput):m_pScreenHandler(pScreenHandler), m_Font(font), m_pWindow(pWindow),m_pMenuInput(pMenuInput), m_pActionInput(pActionInput){};

	~ScreenRegion(){};

};


class BigScreenRegion : public ScreenRegion{
private:
	vector<sf::Vector2i> m_bigScreenPoints;
	LocationHandler * m_pLocationHandler;

	void homeConfig(){

		sf::Text homeTitle;
		homeTitle = SetText(homeTitle, sf::String("Home"), m_Font, white, veryLarge, getPosition(0));
		m_CurrentConfig.push_back(homeTitle);
	}

	void localMapConfig();

public:
	BigScreenRegion(ScreenHandler * pScreenHandler, sf::Font& font, sf::RenderWindow * pWindow, MenuInputHandler * pMenuInput, ActionInputHandler * pActionInput, LocationHandler * pLocationHandler): ScreenRegion(pScreenHandler, font, pWindow, pMenuInput, pActionInput), m_pLocationHandler(pLocationHandler){

          

}

	~BigScreenRegion(){};

	sf::Vector2i getPosition(unsigned short positionID){ return m_bigScreenPoints[positionID]; }

	void setConfiguration(){
		if (m_pLocationHandler->getCurrentLocalLocation() == "Home"){ ///<----This is line the error refers to //error C2227: left of '->getCurrentLocalLocation' must point 
                                                                                                                       //to class/struct/union/generic type
                                                                                                                       //Then
                                                                                                                       //error C2027: Use of undefined type "LocationHandler"
			homeConfig();
		}
	}

	void display(){
		for (vector<sf::Text>::iterator iter = m_CurrentConfig.begin(); iter != m_CurrentConfig.end(); iter++){
			m_pWindow->draw(*iter);
		}
	}

};


Location.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <iostream>
#include <cstdlib>
#include <string>
#include <array>
#include <list>
#include <vector>
#include <algorithm>

using namespace std;

// I believe I defined the class properly...didn't miss anything, I dont think...
class LocationHandler{
private:
	sf::String m_CurrentWorldLocation;
	sf::String m_CurrentLocalLocation;
	sf::String m_Claireville[9];


public:
	LocationHandler(){
		m_Claireville[0] = "Home";

		m_CurrentWorldLocation = "Claireville";
		m_CurrentLocalLocation = m_Claireville[0];
	};

	~LocationHandler(){};

	string getCurrentWorldLocation(){ return m_CurrentWorldLocation; }

	string getCurrentLocalLocation(){ return m_CurrentLocalLocation; }

	void setCurrentLocalLocation(unsigned short newLocationID){
		if (m_CurrentWorldLocation == "Claireville"){
			m_CurrentLocalLocation = m_Claireville[newLocationID];
		}
	}
};


BTW I can never preview what I post before I post it. just in case this comes out looking all jacked up. :p
Last edited on
You need a definition of class to use its members. Not forward declaration. Properly include your class header before use. To avoid circular inclusion problem, usually only implementation file includes appropriate header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//foo.h

class bar;

class foo
{
    bar* b;
    void do();
};

//bar.h
#include "foo.h"

class bar
{
    foo f;
    void do();
}

//foo.cpp
#include "foo.h"
#include "bar.h"

void foo::do()
{
    b->do();
}
I did define the class in the Location header files. The problem was I was not including the headers in the right order. I was including Game.h before Location.h.
Last edited on
The order in which you include headers should never matter - if it does, it means something is wrong with the headers.
I don't know but, that's what fixed it. The code is posted above, see anything wrong with it? Saying "something might be wrong with the headers" is kind of vague. Thanks in advance.
Topic archived. No new replies allowed.