943,722 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 662
  • C++ RSS
Dec 26th, 2008
0

Help with static members

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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)
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 26th, 2008
1

Re: Help with static members

You need to define num as well as declare it:
C++ Syntax (Toggle Plain Text)
  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;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 26th, 2008
0

Re: Help with static members

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 ?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 27th, 2008
1

Re: Help with static members

Click to Expand / Collapse  Quote originally posted by CPPRULZ ...
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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 27th, 2008
0

Re: Help with static members

My real question now is why do I need the syntax number::num; - what does it do physically in the program.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 27th, 2008
0

Re: Help with static members

Click to Expand / Collapse  Quote originally posted by CPPRULZ ...
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).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 27th, 2008
0

Re: Help with static members

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Dec 27th, 2008
0

Re: Help with static members

Line 2 of your second code snippet : #include"headerr.h"
Two rr's?
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 27th, 2008
0

Re: Help with static members

Click to Expand / Collapse  Quote originally posted by CPPRULZ ...
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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Snowflake and recursion problem
Next Thread in C++ Forum Timeline: MSVC++ Express 8 error C2228





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC