Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking problem: unresolved static variable

Status
Not open for further replies.

ajcrm125

Technical User
Jul 25, 2005
1
US
I have a base class Enemy and a derived class Fly (Fly is a type of enemy). Enemy has a static float pointer called spiderTrans:

static float *spiderTrans;

In my main, I create a spider and a fly and try to assign a value to the fly's spiderTrans:

spider = new Spider("./data","spider.png",50.0,50.0);
fly = new Fly("./data","wasp.png",30.0,40.0);

fly->spiderTrans = spider->trans;

(spider has an array of floats called trans)

I get a link error when I try to build:
Webz.obj : error LNK2001: unresolved external symbol "public: static float * Enemy::spiderTrans" (?spiderTrans@Enemy@@2PAMA)
Debug/webz.exe : fatal error LNK1120: 1 unresolved externals

but if I change the variable so that it's not static, it builds fine.
????

I thought the only thing that static meant was that all Enemy's that are created share the same spiderTrans variable.

Thanks for any help!
-Adam
 
You are correct, they all share the same variable. It might be more clear to access it through Enemy::spiderTrans instead of fly->spiderTrans, but you also must define the variable instead of just declare it. So in one of your cpp files, you need to have a line like this:
Code:
float* Enemy::spiderTrans = 0;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top