Jump to content
  • Advertisement
Sign in to follow this  
Jossos

"error LNK2001: unresolved external symbol" for static methods

This topic is 987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have a weird error.

 

Here's my code with a bunch of unimportant stuff cut out:

#pragma once

struct PFNode
{
	int gCost;
	int hCost;
	int fCost;
	int x, z;
	int id;
	PFNode* parent;
	bool blocked;
	bool checked;
};

extern PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];


static void InitPathFinder(World* world)
{
	worldW = world->getTilesWide();
	worldL = world->getTilesLong();

	for (int z = 0; z < worldL; ++z)
	for (int x = 0; x < worldW; ++x)
	{
		map[x][z].x = x;
		map[x][z].z = z;
		map[x][z].id = x + z * worldW;
		map[x][z].blocked = world->getTile(x, z)->isBlocked;
		map[x][z].checked = false;
	}
}

So I get errors saying "1>EM_Component_Mover.obj : error LNK2001: unresolved external symbol "struct PFNode (* map)[256]" (?map@@3PAY0BAA@UPFNode@@A)

1>Game.obj : error LNK2001: unresolved external symbol "struct PFNode (* map)[256]" (?map@@3PAY0BAA@UPFNode@@A)"
 
I have this file included in 2 other files (one being EM_Component_Mover.h) so I'm guessing structs or static methods don't work how I think they do.
 
Any help is greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
If you define a variable (like in this case 'map') as extern you need to put the definition into exactly one implementation file. Currently you tell the compiler "there is a variable called 'map' of such-and-such type somewhere else" but the linker cannot find it anywhere because you never put it anywhere.

Share this post


Link to post
Share on other sites


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

 

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".

Share this post


Link to post
Share on other sites

 


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

 

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".

 

 

 


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

 

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".

 

 

map array is in cpp, and thats not the error im getting. "static PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];"

Share this post


Link to post
Share on other sites


"static PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];"

Do not add "static" there. Static sets internal linkage for map[], so linker won't see that variable available for linking.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!