| | |
error LNK2001: unresolved external symbol - only happens with STATIC variable
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
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!
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:
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: // external declaration static double ratio; }; // actual definition double A::ratio; int main() { A a; a.ratio = 1.76; std::cout << a.ratio << '\n'; }
C++ Syntax (Toggle Plain Text)
#include <iostream> class A { public: static const int Max = 50; }; int main() { A a; std::cout << a.Max << '\n'; }
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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 ?
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.
Re: error LNK2001: unresolved external symbol - only happens with STATIC variable
0
#4 Aug 24th, 2009
•
•
•
•
But until the next C++ standard comes out, that only works with const int static fields
static const int also unsigned and char types are permitted to be declared and defined in the class as static const Example:
C++ Syntax (Toggle Plain Text)
class neFoo { private: static const int a = 9; // ok! static const unsigned int a2 = 9; // ok! static const char b = 'a'; // also ok! static const float c = 2.0; // NOT OK! (not integral) };
Last edited by niek_e; Aug 24th, 2009 at 9:57 am.
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
Re: error LNK2001: unresolved external symbol - only happens with STATIC variable
0
#5 Aug 24th, 2009
Re: error LNK2001: unresolved external symbol - only happens with STATIC variable
0
#6 Aug 24th, 2009
•
•
•
•
That's actually not entirely true. It only works with integral-data types, which means that in addition tostatic const intalso unsigned and char types are permitted to be declared and defined in the class asstatic const
Last edited by Tom Gunn; Aug 24th, 2009 at 10:24 am.
-Tommy (For Great Justice!) Gunn
Re: error LNK2001: unresolved external symbol - only happens with STATIC variable
0
#7 Aug 24th, 2009
![]() |
Similar Threads
- error LNK2001: unresolved external... (C)
- unresolved external symbol error (C++)
- error LNK2001: unresolved external symbol (C++)
- Need help with "error LNK2001 unresolved external symbol" (C++)
- error LNK2001: unresolved external symbol (C++)
- Unresolved External Symbol? (C)
- error LNK2001: unresolved external symbol (C++)
- error LNK2001: unresolved external symbol (C++)
- error LNK2001: unresolved external symbol (C++)
Other Threads in the C++ Forum
- Previous Thread: Deallocating memory used by a STL map
- Next Thread: Starting graphics
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






