null pointer declaration throwing errors c4430, c2061, c2143, c2238

I keep getting a build error when trying to declare a pointer in a class I created, can someone tell me what I'm doing wrong?

Here's the class that needs to declare the pointer (condensed to the pertinent code):

NPC.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
#pragma once
#include <string>
#include <list>
#include <windows.h>
#include <random>
#include "keyword.h"
#include <time.h>
#include "localMap.h"

using namespace std;

class NPC
{
public: 
	string name;

	localMap *region = NULL; //The Errors are here

	void assignRegion(localMap &_region);

	void Live();
	bool alive = true;

	NPC();
};


Here's the localMap class I created, it's very basic right now:
localMap.h
1
2
3
4
5
6
7
8
9
#pragma once
#include "NPC.h"

class localMap
{
public:
	float temperature;
	localMap();
};

localMap.cpp
1
2
3
4
5
6
#include "localMap.h"

localMap::localMap()
{
	temperature = 20.0f;
}


I create the localMap object in another script that gets passed into the NPC script using assignRegion...I'm also wondering if I did that code correctly? here it is:

assignRegion (part of NPC.cpp)
1
2
3
4
void NPC::assignRegion(localMap & _region)
{
	region = &_region;
}
Last edited on
You have recursive #includes.
npc.h includes localmap.h
localmap.h includes npc.h

Why does localmap.h include npc.h? It doesn't use it.
Remove that include.

If you truly need class A to reference class B and class B to reference class A, you must use pointers and a forward declaration for one of the classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//  A.h
class B;	//	Forward declaration

class A
{
	B* m_pb;	// member pointer to B
				//  okay because it's a pointer
};

//	B.h
#include "a.h"	// okay if one file includes the other
				// but not both including each other

class B
{
	A* m_pa;	//  member pointer to A
};

Last edited on
wow. Thanks for the help--I took the #include "NPC.h" from the localMap.h file and it got rid of the errors--I can't remember why I was including that in the first place tbh. I think my intention was probably to do the reverse:
Basically, I'm having the NPC point to a region that it is associated with (that it resides in) and any functions I need to do to adjust the region can be handled there, but I think at one point I was planning to do the reverse and have the region hold onto the NPCs that reside in it. --I may have left that in there thinking that I would want to do both, in which case your example is really helpful if I need to do both.
Topic archived. No new replies allowed.