Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mfc static member objects 1

Status
Not open for further replies.

EEEeee

Programmer
May 1, 2001
6
GB
Hi All,

Why is it that within a class declaration I can write :

static CString str;

but I can't write :

static CObList lst;

without getting a link error ?

Any help greatly appreciated.

Regards,

EEEeee...
 
I had tried the:
#include <afxcoll.h> //in stdafx.h

class xxxx{
...
static CObList lst;
...
};

And I got no link error.
I tried this inside a MFC EXE project.

Maybe this helps you,s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
OK, here's what I have :

class CRecorder
{
public:
CRecorder(DEVICE *pDevice);
virtual ~CRecorder();

static CObList FreeDevs;
static CObList BusyDevs;

// Various other member declarations.
};

The app is a console app with MFC support (to be turned into a service once complete & tested).

A number of CRecorder instances are to be created at run time.

The errors I receive are as follows :

Main.obj : error LNK2001: unresolved external symbol &quot;public: static class CObList CRecorder::BusyDevs&quot;
Main.obj : error LNK2001: unresolved external symbol &quot;public: static class CObList CRecorder::FreeDevs&quot;

I have afxcoll.h included, no idea why the unresolved externals, if I remove the static keyword everything links fine.

Please help !!!

Regards,

EEEeee...
 
It is because you must initialize the static variable in that object:

class x
{
public:
static int c;
};


int x::c=1;//if you don't do it, you'll get a linker error


int main()
{
x t;
t.c=0;
return 0;
}

John Fill
1c.bmp


ivfmd@mail.md
 
It's not a static var though, it's a static object.

Further to this, if I add :

static CString str;

into the class definition and compile - no problemo, however as soon as I reference CRecorder::str I get the same link error as before.

R's,

EEEeee...
 
My project still work. I have VisualC++ 6.0 with SP3 on NT4.0.

I had created a new empty project of type console aplication, I have chosen the last option with mfc support and it works. Below are my .h file and stdafx.h file:

#include <afxcoll.h>
class CRecorder
{
public:
CRecorder();
virtual ~CRecorder();

static CObList FreeDevs;
static CObList BusyDevs;
};

//stdafx.h
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <iostream>
////////////////////////

You should have make your project work, by now.
Hope this is getting you out of trouble. s-)

Blessed is he who in the name of justice and good will, shepards the week through the valley of darknees...
 
Thanks BurtanI, does your example link when you reference the static members from other code ?

R's,

E.
 
Thanks JohnFill, with a simple...

CObList CRecorder::BusyDevs;

...all is now well with my world.

R's,

E.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top