error LNK2001: unresolved external symbol - only happens with STATIC variable

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 3
Reputation: larrifari is an unknown quantity at this point 
Solved Threads: 0
larrifari larrifari is offline Offline
Newbie Poster

error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #1
Aug 24th, 2009
Hi,

I am still a newbie to C++ and ran into a strange problem - I am using a static instance variable in my class, as soon as I do so I get an

error LNK2001: unresolved external symbol error message - however this only occurs when I am trying to set it to static, not when I just leave it non-static.

I need the variable to be static and dont know how to fix the problem. I did some searches and got an idea that this might either have to do with some compiler flags that need to be changed (however I only know how to ADD flags to the command line, not how to REMOVE them in Visual Studio Espress 2008) ... or with some C libraries which require including for this problem to disappear.

Can somebody help me with this and explain in more beginners-terms what I need to do, I find this slightly overwhelming.

I am using Visual Studio Express 2008 and the CImg library

Many thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #2
Aug 24th, 2009
Static fields in a class are really external declarations. You need to define the field outside of the class for it to exist:
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. // external declaration
  7. static double ratio;
  8. };
  9.  
  10. // actual definition
  11. double A::ratio;
  12.  
  13. int main()
  14. {
  15. A a;
  16.  
  17. a.ratio = 1.76;
  18. std::cout << a.ratio << '\n';
  19. }
If you remove the actual definition, building the code will give you an unresolved external object error. For static ints that are const, you can initialize them inline and that counts as a definition. But until the next C++ standard comes out, that only works with const int static fields:
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. static const int Max = 50;
  7. };
  8.  
  9. int main()
  10. {
  11. A a;
  12.  
  13. std::cout << a.Max << '\n';
  14. }
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: larrifari is an unknown quantity at this point 
Solved Threads: 0
larrifari larrifari is offline Offline
Newbie Poster

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #3
Aug 24th, 2009
Oh my god (sorry, so cheesy!) but you just released me from 3 days of torture ... no wonder I was not finding a solution, I thought I had a different problem.

THANK YOU.

Is this generally necessary or just for Visual Studio C++ ? I seem to remember that when using Borland and a text editor it worked without an external declaration? In fact it seems to work fine even in VS if I dont declare a CImg but a "normal" type such as a double or an int ?
Last edited by larrifari; Aug 24th, 2009 at 9:53 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 307
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #4
Aug 24th, 2009
Originally Posted by Tom Gunn View Post
But until the next C++ standard comes out, that only works with const int static fields
That's actually not entirely true. It only works with integral-data types, which means that in addition to static const int also unsigned and char types are permitted to be declared and defined in the class as static const
Example:
  1. class neFoo {
  2. private:
  3. static const int a = 9; // ok!
  4. static const unsigned int a2 = 9; // ok!
  5. static const char b = 'a'; // also ok!
  6. static const float c = 2.0; // NOT OK! (not integral)
  7. };
Last edited by niek_e; Aug 24th, 2009 at 9:57 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: larrifari is an unknown quantity at this point 
Solved Threads: 0
larrifari larrifari is offline Offline
Newbie Poster

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #5
Aug 24th, 2009
aah okay, here is the answer already. Makes sense.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #6
Aug 24th, 2009
Originally Posted by niek_e View Post
That's actually not entirely true. It only works with integral-data types, which means that in addition to static const int also unsigned and char types are permitted to be declared and defined in the class as static const
It is true when you read 'int' as integer type. I had a brain fart and forgot that you guys do not interpret normal prose and need everything spelled out in standardese.
Last edited by Tom Gunn; Aug 24th, 2009 at 10:24 am.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 307
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: error LNK2001: unresolved external symbol - only happens with STATIC variable

 
0
  #7
Aug 24th, 2009
Originally Posted by Tom Gunn View Post
It is true when you read 'int' as integer type. I had a brain fart and forgot that you guys do not interpret normal prose and need everything spelled out in standardese.
Nitpicking is one of my favorite hobbies here on Daniweb
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC