![]() |
|
#1
|
|||
|
|||
Hernia Over Static Variables in ClassesHi,
I'm pretty new to C++ and have had most of my experience in Java. I have been trying for a good bit now to use a static variable that I can increment for all objects within a class. Here's a look at the stripped out code: CPP / C++ / C Code:
I get: DumbLife error LNK2019: unresolved external symbol "private: static int Blob::history" (?history@Blob@@0HA) referenced in function "public: int __thiscall Blob::birthBlob(int,int,int,int,double,double,int, int,int,double,int,double,double,int,int)" (?birthBlob@Blob@@QAEHHHHHNNHHHNHNNHH@Z) If you could help me understand how to do this in C++ I would appreciate it very much. Thank you, Louis S. |
|
#2
|
|||
|
|||
|
#3
|
|||
|
|||
|
Hi,
1. Static members are not related to a particular class..... 2. it should be initialized outside the class....using scope resolution operator. 3. Initialized only once... Ex.(From MSDN) CPP / C++ / C Code:
.....am i clear...?? or i'm wrong.... Best Regards, Janakiraman Last edited by LuciWiz : 04-May-2005 at 00:47.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
|||
|
|||
|
Janakiraman, I tried like in your MSDN exampe with initialization at the end of the class and now I get:
DumbLife error LNK2005: "private: static int Blob::history" (?history@Blob@@0HA) already defined in Blob.obj Can I only access static variables inside of static functions? |
|
#5
|
|||
|
|||
|
CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
|
Dave Sinkula,
I tried doing it as in your example but now I recieve a linker error saying it is overdefined or defined more than once. Thanks, Louis |
|
#7
|
|||
|
|||
|
>"as in your example"
Copy and paste. Either copy and paste what I wrote and try to build it, or copy and paste what you have here so that I can try it. |
|
#8
|
|||
|
|||
|
CPP / C++ / C Code:
And I recieve the following error: * DumbLife error LNK2005: "private: static int Blob::history" (?history@Blob@@0HA) already defined in Blob.obj * DumbLife fatal error LNK1169: one or more multiply defined symbols found The code I have entered is very stripped out. Thank you, Louis |
|
#9
|
|||
|
|||
|
Quote:
I'll take a guess: is the class definition and the static member initialization in a header file that is included by more than file? http://msdn.microsoft.com/library/de...ml/LNK2005.asp Last edited by Dave Sinkula : 03-May-2005 at 13:14.
Reason: Added MSDN link.
|
|
#10
|
|||
|
|||
|
Actually it is inside a .cpp file.
|
Recent GIDBlog
A Week in Kuwait by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| passing variables out of an iframe by url | JUNK KED | MySQL / PHP Forum | 5 | 31-Jul-2007 09:33 |
| Assistance with classes... | Bravebird | C++ Forum | 7 | 27-Apr-2005 13:17 |
| changing caption of static text control | Pandiani | MS Visual C++ / MFC Forum | 1 | 20-Sep-2004 01:38 |
| A Comprehensive Digest of C++ | mithunjacob | C++ Forum | 39 | 20-Jun-2004 19:09 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The