Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Problems inheriting multiple times.

Reply
Thread Tools

Re: Problems inheriting multiple times.

 
 
Snyke
Guest
Posts: n/a
 
      08-23-2004
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@@@@1PAVStatis tics@@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


 
Reply With Quote
 
 
 
 
Denis Remezov
Guest
Posts: n/a
 
      08-23-2004
Snyke wrote:
>
>
> Anyway, my Singleton Header is this:
> [code]


[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)):

> /// 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
>



Denis
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-23-2004
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_


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Problems with multiple multiple-level inclusion of DTDs Peter Flynn XML 1 07-27-2008 08:14 PM
Opinions please - using placeholders vs inheriting for multiple presentation Alan Silver ASP .Net 3 07-25-2005 03:13 PM
Problems inheriting multiple times. Snyke C++ 1 08-23-2004 01:40 PM
More problems inheriting from streambuf Christopher Benson-Manica C++ 3 02-26-2004 04:11 PM
Problems with viewing inheriting Properties in VS.NET design time Dann Pool ASP .Net Web Controls 0 11-11-2003 07:11 PM



Advertisments