Public Group

"error LNK2001: unresolved external symbol" for static methods

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

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 on other sites
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 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 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 on other sites

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

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633665
• Total Posts
3013247
×