| | |
Help with static members
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
Hi, I was making a pogram to demostrate static variables and I don't know why my compiler (VS 2005) is giving me these crazy errors that my book im using said I shouldn't be getting. Here is my program, please help:
header:
body (.cpp)
This code returns the errors:
body.obj : error LNK2019: unresolved external symbol "private: static int number::num" (?num@number@@0HA) referenced in function "public: static int __cdecl number::getnum(void)" (?getnum@number@@SAHXZ)
1>C:\Documents and Settings\Computer stuff man\Desktop\test progams\classes 3.0\Debug\classes 3.0.exe : fatal error LNK1120: 1 unresolved externals
header:
C++ Syntax (Toggle Plain Text)
class number{ private: static int num; public: int getnum() {return num;} void setnum(int innum) {num=innum;} };
C++ Syntax (Toggle Plain Text)
#include<iostream> #include"headerr.h" using namespace std; int main() { int innum=0; class number test; test.setnum(10); cout<<test.getnum(); system("pause"); return (0); }
This code returns the errors:
body.obj : error LNK2019: unresolved external symbol "private: static int number::num" (?num@number@@0HA) referenced in function "public: static int __cdecl number::getnum(void)" (?getnum@number@@SAHXZ)
1>C:\Documents and Settings\Computer stuff man\Desktop\test progams\classes 3.0\Debug\classes 3.0.exe : fatal error LNK1120: 1 unresolved externals
You need to define num as well as declare it:
C++ Syntax (Toggle Plain Text)
class number{ private: static int num; public: int getnum() {return num;} void setnum(int innum) {num=innum;} }; int number::num;
I'm here to prove you wrong.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
Does it matter where you do this (int number::num) because I tried it out in the main and it works now but why? It doesn't seem to do anything other than maybe introduce it into a new block however you put it in the header and it still works. So what does it really do ?
Placing the definition in your header file will violate the ODR if more than one source file in your project #include's it.
Right 98% of the time, and don't care about the other 3%.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
My real question now is why do I need the syntax number::num; - what does it do physically in the program.
The "static int num;" within the body of class number declares the static member. The statement outside the class body "int number::num;" defines it.
Declaring a static member of a class means that it is known to exist, so code can reference it. Defining a static means allocating storage for it so, when a program is linked, all references in code to that static member resolve to the same object (i.e. the same area of physical memory).
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
•
•
•
•
So, just to check, int number::num; clears a place to store data at a certain data location-just like when you declare a class variable you give all of the variables in the class a place in physical memory and an adress however the adress holds NULL.
The declaration "static int num;" within the body of class number simply tells the compiler that a single integer named number::num exists somewhere in memory.
The definition "int number::num;" ensures that memory is actually allocated to hold (or represent) that single integer.
Right 98% of the time, and don't care about the other 3%.
![]() |
Similar Threads
- problem regarding static members in structure (C++)
- about static variable (Java)
- static problem (C)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: Snowflake and recursion problem
- Next Thread: MSVC++ Express 8 error C2228
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






