Re: Problems inheriting multiple times.

Discussion in 'C++' started by Snyke, Aug 23, 2004.

  1. Snyke

    Snyke Guest

    Uhm... didn't get the point of Unit.h but I hope it's a joke :)

    Anyway, my Singleton Header is this:
    Code:
    #ifndef __SINGLETON_H__
    #define __SINGLETON_H__
    
    #include <assert.h>
    #include <stdio.h>
    
    // TODO: handle errors better
    
    // An assert isn't necessarily fatal, although if compiled with asserts
    enabled it will be.
    #define WPAssert( assertion ) { if( !(assertion) ) { fprintf( stderr,
    "\n%s:%i ASSERTION FAILED:\n  %s\n", __FILE__, __LINE__, #assertion );
    assert( #assertion &&0 ); } }
    #define WPError( assertion, errmsg ) if( ! (assertion) ) {
    Log::getSingleton( ).outError( "%s:%i ERROR:\n  %s", __FILE__,
    __LINE__, (char *)errmsg ); assert( false ); }
    #define WPWarning( assertion, errmsg ) if( ! (assertion) ) {
    Log::getSingleton( ).outError( "%s:%i WARNING:\n  %s", __FILE__,
    __LINE__, (char *)errmsg ); }
    
    // This should always halt everything.  If you ever find yourself
    wanting to remove the assert( false ), switch to WPWarning or WPError
    #define WPFatal( assertion, errmsg ) if( ! (assertion) ) {
    Log::getSingleton( ).outError( "%s:%i FATAL ERROR:\n  %s", __FILE__,
    __LINE__, (char *)errmsg ); assert( #assertion &&0 );
    Threads::getSingleton( ).closeCurrentThread( ); }
    
    /// Should be placed in the appropriate .cpp file somewhere
    #define initialiseSingleton( type ) template < > type * Singleton <
    type > :: mSingleton = 0
    
    /// To be used as a replacement for initialiseSingleton( )
    ///  Creates a file-scoped Singleton object, to be retrieved with
    getSingleton
    #define createFileSingleton( type ) initialiseSingleton( type ); type
    the##type
    
    template < class type > class Singleton {
    public:
    /// Constructor
    Singleton( ) {
    /// If you hit this assert, this singleton already exists -- you
    can't create another one!
    WPAssert( mSingleton == 0 );
    mSingleton = ( type * ) this;
    }
    /// Destructor
    ~Singleton( ) { }
    
    /// Retrieve the singleton object, if you hit this assert this
    singleton object doesn't exist yet
    static type & getSingleton( ) { WPAssert( mSingleton ); return
    *mSingleton; }
    
    /// Retrieve a pointer to the singleton object
    static type * getSingletonPtr( ) { return mSingleton; }
    
    protected:
    
    /// Singleton pointer, must be set to 0 prior to creating the object
    static type * mSingleton;
    };
    
    #endif
    
    While the classes inheriting this class are like this:
    Code:
    #ifndef __CONFIGURATION_H__
    #define __CONFIGURATION_H__
    
    #include "Xml/ParamIO.h"
    #include "include/singleton.h"
    #include "include/global.h"
    #include <string>
    using namespace std;
    
    class configuration : public Singleton < configuration > {
    public:
    bool fetchLinkSrc;
    int sleepTime;
    void print();
    bool runAndQuit;
    string master;
    configuration(string filename = "config.xml");
    virtual ~configuration();
    };
    
    #endif // __CONFIGURATION_H__
    
    The configuration class works fine but if I then add another inherited
    class like:
    Code:
    #ifndef __STATISTICS_H__
    #define __STATISTICS_H__
    
    #include "include/Singleton.h"
    
    class Statistics : public Singleton   < Statistics >{
    public:
    Statistics();
    virtual ~Statistics();
    
    };
    
    #endif
    
    (Notice that this is only the skeleton but it already gives the
    linker error...)

    I get this error:
    Statistics.obj : error LNK2001: unresolved external symbol "protected:
    static class Statistics * Singleton<class Statistics>::mSingleton"
    (?mSingleton@?$Singleton@VStatistics@@@@1PAVStatistics@@A)
    Debug/EasyEarn Concept.exe : fatal error LNK1120: 1 unresolved
    externals
    Error executing link.exe.

    Thomas Matthews wrote:
    > Snyke wrote:
    > > Well the title says it all:
    > > I'm trying to inherit a Class (a Singleton class) twice.
    > > The only problem is that I get a linker problem: it says it can't

    find
    > > the external depency for the static pointer to the existing

    instance.
    > > This only occurs when I try to build my second or third class, the
    > > first inherited class works fine.
    > > Anybody got an idea???
    > >

    >
    > Show
    > Some
    > Code.
    >
    > Your problem is on line 42 of Unit1.cpp. I believe
    > you want to remove the ";".
    >
    >
    > --
    > Thomas Matthews
    >
    > C++ newsgroup welcome message:
    > http://www.slack.net/~shiva/welcome.txt
    > C++ Faq: http://www.parashift.com/c -faq-lite
    > C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    > alt.comp.lang.learn.c-c++ faq:
    > http://www.comeaucomputing.com/learn/faq/
    > Other sites:
    > http://www.josuttis.com -- C++ STL Library book
    Snyke, Aug 23, 2004
    #1
    1. Advertising

  2. Snyke wrote:
    >
    >
    > Anyway, my Singleton Header is this:
    >
    Code:
    [/color]
    
    [snip]
    
    You didn't provide a definition for mSingleton when you tried to use
    Singleton <Statistics>, that is why you are getting linking errors.
    
    Someone (perhaps the author of the Singleton class template) put a few
    comments in the file singleton.h.  The comments explain how Singleton was
    intended to be used (by the way, I personally don't like this design
    (namely the use of the preprocessor)):
    [color=blue]
    > /// Should be placed in the appropriate .cpp file somewhere
    > #define initialiseSingleton( type ) template < > type * Singleton <
    > type > :: mSingleton = 0
    >
    > /// To be used as a replacement for initialiseSingleton( )
    > ///  Creates a file-scoped Singleton object, to be retrieved with
    > getSingleton
    > #define createFileSingleton( type ) initialiseSingleton( type ); type
    > the##type
    >[/color]
    
    
    Denis
    Denis Remezov, Aug 23, 2004
    #2
    1. Advertising

  3. Snyke

    red floyd Guest

    Snyke wrote:
    >[redacted]


    Your include guards are in violation of the Standard. All identifiers
    containing two consecutive underscores are reserved to the implementation.

    Instead of using

    #ifndef __MYGUARD__

    try using

    #ifndef MYGUARD_

    or my personal preference

    #ifndef MY_INCLUDE_H_
    red floyd, Aug 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. djskrill
    Replies:
    9
    Views:
    687
    djskrill
    Oct 1, 2003
  4. Snyke
    Replies:
    1
    Views:
    319
    Thomas Matthews
    Aug 23, 2004
  5. Jack Steven
    Replies:
    2
    Views:
    416
    Chris Rebert
    Mar 9, 2009
Loading...

Share This Page