Static set member.

Ravishankar S ravishankar.s@hathway.com
Fri Sep 1 15:44:00 GMT 2006


u have to provide definitions for static members explicitly.

Example:

x.h:

class X {
   private:
      static int x; // static member declaration
   public:
     void f() {
        cout << "static X::x is " << X::x << endl;
     }

};

x.cpp: 
int X::x; // static member definiton

Refer to CPP FAQ by Marshall Cline and Greg Lomow.

-Ravishankar

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Smith-Rowland, Edward M
Sent: Wednesday, August 30, 2006 3:44 AM
To: Young, Michael; gcc-help@gcc.gnu.org; 3dw4rd@verizon.net
Subject: RE: Static set member.


What a maroon!

Thanks for the reply.

I actually had a *different* problem that this example was supposed to
show. I have attached the new sources (with the obvious problem fixed
:-P).

The sources compile but the linking fails!
[ed@localhost ~]$ g++ thing.cpp
/tmp/ccAhcYMW.o(.text+0x11): In function `ThingManager::show(Thing*)':
: undefined reference to `ThingManager::thingSet'
/tmp/ccAhcYMW.o(.text+0x35): In function `ThingManager::hide(Thing*)':
: undefined reference to `ThingManager::thingSet'
/tmp/ccAhcYMW.o(.text+0x49): In function `ThingManager::hideAll()':
: undefined reference to `ThingManager::thingSet'
/tmp/ccAhcYMW.o(.text+0x57): In function `ThingManager::hideAll()':
: undefined reference to `ThingManager::thingSet'
/tmp/ccAhcYMW.o(.text+0x6d): In function `ThingManager::hideAll()':
: undefined reference to `ThingManager::thingSet'
collect2: ld returned 1 exit status

Somehow, All the classes are correct but the static object is not
getting instantiated.


BTW: I realize the static thing in NOT thread safe.


-----Original Message-----
From: Young, Michael [mailto:Michael.Young@paetec.com]
Sent: Tue 8/29/2006 5:37 PM
To: Smith-Rowland, Edward M; gcc-help@gcc.gnu.org
Subject: RE: Static set member.
 
In ThingManager, you have a thingSet member, not a thingList.  Your
function definitions reference a "thingList", which is not defined.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
Behalf Of Smith-Rowland, Edward M
Sent: Tuesday, August 29, 2006 5:17 PM
To: gcc-help@gcc.gnu.org
Subject: Static set member.


All,

I am attempting to create a singleton class something like this:

class ThingManager
{

    static void show( Thing * thng );
    static void hide( Thing * thng );
    static void hideAll( void );

private:

    static std::set<Thing *,ThingComp> thingSet;
};

I get these errors:
[ed@localhost ~]$ g++ thing.cpp
thing.cpp: In static member function `static void
ThingManager::show(Thing*)':
thing.cpp:7: error: `thingList' was not declared in this scope
thing.cpp: In static member function `static void
ThingManager::hide(Thing*)':
thing.cpp:13: error: `thingList' was not declared in this scope
thing.cpp: In static member function `static void
ThingManager::hideAll()':
thing.cpp:19: error: `thingList' was not declared in this scope

More details (still simple) in the attached files.

What am I doing wrong???

Adding a constructor or initializer to the cpp file is an error:
std::set<Thing *,ThingComp> ThingManager::thingSet();

Ed






__________ NOD32 1.1454 (20060321) Information __________

This message was checked by NOD32 antivirus system. http://www.eset.com




More information about the Gcc-help mailing list