Help with static members

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

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Help with static members

 
0
  #1
Dec 26th, 2008
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:
  1. class number{
  2. private:
  3. static int num;
  4. public:
  5. int getnum()
  6. {return num;}
  7. void setnum(int innum)
  8. {num=innum;}
  9. };
body (.cpp)
  1. #include<iostream>
  2. #include"headerr.h"
  3. using namespace std;
  4. int main()
  5. {
  6. int innum=0;
  7. class number test;
  8. test.setnum(10);
  9. cout<<test.getnum();
  10. system("pause");
  11. return (0);
  12. }

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help with static members

 
1
  #2
Dec 26th, 2008
You need to define num as well as declare it:
  1. class number{
  2. private:
  3. static int num;
  4. public:
  5. int getnum()
  6. {return num;}
  7. void setnum(int innum)
  8. {num=innum;}
  9. };
  10.  
  11. int number::num;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with static members

 
0
  #3
Dec 26th, 2008
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 ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Help with static members

 
1
  #4
Dec 27th, 2008
Originally Posted by CPPRULZ View Post
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 ?
It doesn't matter where you do it, as long as you obey the one-definition rule (ODR). Typical projects link together object multiple object files to create an executable, and each object file is created from a single source file. If you have two or more source files in your project, then only one of them can define number::num.

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%.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with static members

 
0
  #5
Dec 27th, 2008
My real question now is why do I need the syntax number::num; - what does it do physically in the program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Help with static members

 
0
  #6
Dec 27th, 2008
Originally Posted by CPPRULZ View Post
My real question now is why do I need the syntax number::num; - what does it do physically in the program.
Narue answered that already.

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).
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: Help with static members

 
0
  #7
Dec 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,974
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 287
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Help with static members

 
0
  #8
Dec 27th, 2008
Line 2 of your second code snippet : #include"headerr.h"
Two rr's?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Help with static members

 
0
  #9
Dec 27th, 2008
Originally Posted by CPPRULZ View Post
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.
Sorry, that statement strikes me as gibberish.

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%.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC