GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2005, 21:12
lsimons lsimons is offline
New Member
 
Join Date: May 2005
Posts: 6
lsimons is on a distinguished road

Hernia Over Static Variables in Classes


Hi,

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:
class Blob
{

private:
     static int history; // history will need to be incremented from time to time, not necessarily at construction

private:

Blob()
{}

int birthBlob()
{
Blob::history++;
}

}

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  
Old 01-May-2005, 21:32
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
  #3  
Old 02-May-2005, 05:59
Janakiraman Janakiraman is offline
Junior Member
 
Join Date: Apr 2005
Location: Chennai - India
Posts: 47
Janakiraman is on a distinguished road
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:
class SavingsAccount
{
public:
   static void setInterest( float newValue )  // Member function
      { currentRate = newValue; }             //    that accesses
                                              //    only static
                                              //    members
private:
   char name[30];
   float total;
   static float currentRate;    // One copy of this member is
                                //    shared among all instances
                                //    of SavingsAccount
};

// Static data members must be initialized at file scope, even
//    if private.
float SavingsAccount::currentRate = 0.00154;

.....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  
Old 02-May-2005, 09:26
lsimons lsimons is offline
New Member
 
Join Date: May 2005
Posts: 6
lsimons is on a distinguished road
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  
Old 02-May-2005, 10:09
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
CPP / C++ / C Code:
#include <iostream>

class T
{
   static int instances;
public:
   T()  { ++T::instances; }
   ~T() { --T::instances; }
   void show(const char *label = "") const
   {
      std::cout << label << "[member] instances = " << instances << std::endl;
   }
   friend void show(const char *label = "")
   {
      std::cout << label << "[friend] instances = " << T::instances << std::endl;
   }
};

int T::instances = 0;

int main()
{
   show("1:");
   {
      T a, b;
      show("2:");
      {
         T c;
         show("3:");
         c.show("4:");
         T d;
         show("5:");
         d.show("6:");
      }
      show("7:");
   }
   show("8:");
   return 0;
}

/* my output
1:[friend] instances = 0
2:[friend] instances = 2
3:[friend] instances = 3
4:[member] instances = 3
5:[friend] instances = 4
6:[member] instances = 4
7:[friend] instances = 2
8:[friend] instances = 0
*/
  #6  
Old 03-May-2005, 11:36
lsimons lsimons is offline
New Member
 
Join Date: May 2005
Posts: 6
lsimons is on a distinguished road
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  
Old 03-May-2005, 11:59
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
>"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  
Old 03-May-2005, 12:34
lsimons lsimons is offline
New Member
 
Join Date: May 2005
Posts: 6
lsimons is on a distinguished road
CPP / C++ / C Code:
class Blob
{

static int history;  // number of blobs ever

};

int Blob::history=0;


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  
Old 03-May-2005, 12:47
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by lsimons
The code I have entered is very stripped out.
Well, the devil is in the details, and the error is not in what you have posted -- it is in what you haven't.

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  
Old 03-May-2005, 18:24
lsimons lsimons is offline
New Member
 
Join Date: May 2005
Posts: 6
lsimons is on a distinguished road
Actually it is inside a .cpp file.
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 23:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.