RE: Static set member.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx]
Sent: Tue 8/29/2006 5:37 PM
To: Smith-Rowland, Edward M; gcc-help@xxxxxxxxxxx
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@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx]On
Behalf Of Smith-Rowland, Edward M
Sent: Tuesday, August 29, 2006 5:17 PM
To: gcc-help@xxxxxxxxxxx
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




#include <set>

class Thing
{

public:

    Thing( void ) : iThing(42) {}
    int get( void ) const { return iThing; }

private:

    int iThing;
};


struct ThingComp
{
    bool operator()( Thing * thing1, Thing * thing2 )
    {
        return (thing1->get() < thing2->get());
    }
};


class ThingManager
{

public:

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

private:

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

#include "thing.h"


void ThingManager::show( Thing * thing )
{
    thingSet.insert( thing );
}


void ThingManager::hide( Thing * thing )
{
    thingSet.erase( thing );
}


void ThingManager::hideAll( void )
{
    thingSet.erase( thingSet.begin(), thingSet.end() );
}


int main( int, char ** )
{

    ThingManager::show( new Thing() );

    return 0;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux