static data members of class

Posted on 1998-10-13
Last Modified: 2010-04-02
I wish to keep track of object and any objects derived from it. I was trying to use a static data member of a class to do this but during link time I get an unresolved reference. Can anyone show me how to do this ?.
Question by:BlackRussian

Expert Comment

ID: 1175055
are you looking to solve the unresolved reference error (in which case, please show us your code), or would you like to know how to track an object please tell us. What exactly - (whether you are tracking the creation of an object or somehting else, etc).
LVL 23

Expert Comment

ID: 1175056
You should define the static data member once somewhere.

For example,

// header file
class CA
    static int m_nCount;

// source file
// define and initialize it
int CA::m_nCount = 0;


Expert Comment

ID: 1175057
Does your application use multithreading?  You will surely need critical sections to access the shared static data member. That is what I am used too.   Also, you must give more explanation of your problem and your needs.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Expert Comment

ID: 1175058
Does your application use multithreading?  You will surely need critical sections to access the shared static data member. That is what I am used too.   Also, you must give more explanation of your problem and your needs.

Author Comment

ID: 1175059
here is the class declaration

class Cdr : public CChildFrame
      Cdr();           // protected constructor used by dynamic creation
      DispPage disp_page;
// Attributes
      CDaoRecordset *cdr_recordset;            // recordset object
      CDaoRecordset *pinphone_recordset;
      CDaoRecordset *pinid_recordset;
      CdrView *CdrViewptr;
      CString SQLstmt;
      int      iSelectionCnt;
      BOOL bSearchInitiated;
      static int iObjectCntr; <-----------
// Operations

// Overrides
      // ClassWizard generated virtual function overrides

// Implementation
      virtual ~Cdr();
      BOOL CheckDisposition (CString *pSQLStmt);
      BOOL CheckDisposition (void);
      int ExecuteCdrSearch (void);

      // Generated message map functions
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      afx_msg void OnMDIActivate(BOOL bActivate, CWnd*

here is constructor
 // initialize recordset objects associated
 // with cdr class
 cdr_recordset = NULL;            
 pinphone_recordset = NULL;
 pinid_recordset = NULL;
 iObjectCntr = 0;

Note I still get unresolved external symbol error during link

Accepted Solution

Answers2000 earned 100 total points
ID: 1175060
int the .cpp file add this line (outside any function bodies)

int Cdr::iObjectCntr;

1) The static definition in the class defines the definition of the class as including the member, but doesn't allocate storage.

2) The above line, allocates the storage.  You need to do things like this for all static members of classes.

Failure to do #2 is what causes unresolved external error in linking
LVL 23

Expert Comment

ID: 1175061
What's wrong with my answer? Answers2000's answer is the same as mine.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fast, free, private online code repository / editor 7 106
max float value 3 51
How do I get Window Title of all opened process? 4 14
learn programming 8 42
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question