Solved

static data members of class

Posted on 1998-10-13
7
187 Views
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 ?.
0
Comment
Question by:BlackRussian
7 Comments
 

Expert Comment

by:jbalagop
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).
0
 
LVL 23

Expert Comment

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

For example,

// header file
class CA
{
private:
    static int m_nCount;
};

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

0
 
LVL 1

Expert Comment

by:msa092298
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:msa092298
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.
0
 

Author Comment

by:BlackRussian
ID: 1175059
here is the class declaration

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

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(Cdr)
      //}}AFX_VIRTUAL

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

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

here is constructor
Cdr::Cdr()
{
 // 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
0
 
LVL 8

Accepted Solution

by:
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
0
 
LVL 23

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now