Singleton Problem



  • Hallo, ich hab da ein kleines Problem mit einer Singletonklasse.
    Beim kompilieren bringt der compiler (MinGW-Code::Blocks) folgenden Fehler:

    obj\Debug\PPort.o:: In function ZN5PPortC2Ev': P:\\CPP\\Programme\\KameraSteuerung\\wx\\PPort.cpp:4: multiple definition ofPPort::pInstance'
    :: === Build finished: 1 errors, 0 warnings ===

    Vieleicht kann mir jemand weiterhelfen, denn ich steh grad irgendwie aufm Schlauch und komm net weiter 😕

    Hier ist der Quelltext der Klasse:
    PPort.h:

    #ifndef PPORT_H_INCLUDED
    #define PPORT_H_INCLUDED
    
    #include <windows.h>
    
    typedef short _stdcall (*inpfuncPtr)(short portaddr);
    typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
    
    const short OFF = 0;
    const short FOCUS = 1;
    const short SHOT = 2;
    const short FOCUSSHOT = 3;
    
    class PPort
    {
    public:
        static PPort* Instance();
        void out(const short &byte);
        short in();
    
    private:
        PPort();
        PPort(const PPort&);
        ~PPort();
    
        static PPort *pInstance;
    
        PPort *pPPort;
        HINSTANCE hLib;
        inpfuncPtr inp32;
        oupfuncPtr oup32;
    
        class Guard
        {
        public:
            ~Guard()
            {
                if( PPort::pInstance != NULL )
                {
                    delete PPort::pInstance;
                    PPort::pInstance = NULL;
                }
            }
        };
        friend class Guard;
    };
    
    PPort* PPort::pInstance = 0;
    
    #endif // PPORT_H_INCLUDED
    

    PPort.cpp:

    #include "PPort.h"
    
    PPort::PPort()
    {
        hLib = LoadLibrary("inpout32.dll");
        inp32 = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
        oup32 = (oupfuncPtr) GetProcAddress(hLib, "Out32");
    }
    
    PPort::~PPort()
    {
        (oup32)(0x378,0);
        FreeLibrary(hLib);
    }
    
    PPort* PPort::Instance()
    {
            static Guard g;
            if(!pInstance)
                pInstance = new PPort();
            return pInstance;
    }
    
    void PPort::out(const short &byte)
    {
        (oup32)(0x378,byte);
    }
    
    short PPort::in()
    {
        return (inp32)(0x378);
    }
    

    PPort::Instance()->out(OFF);
    ...



  • #if !defined (SINGLETON_H__INCLUDED)
    #define SINGLETON_H__INCLUDED
    
    #if (_MSC_VER > 1000)
    #pragma once
    #endif 
    
    namespace data
    {
    	template <class T>
    	class Singleton
    	{
    	public:
    		static T& instance() { static T inst; return inst; }
    
    	protected:
    		Singleton() {}
    		~Singleton() {}
    		Singleton& operator=(const Singleton&) { return *this; }
    	};
    };
    #endif // SINGLETON_H__INCLUDED
    

    ...

    class A : public data::Singleton<A>
    {
    };
    

    ...



  • pack das

    CKartoffel schrieb:

    PPort* PPort::pInstance = 0;
    

    ins cpp.



  • (D)Evil schrieb:

    #if !defined (SINGLETON_H__INCLUDED)
    #define SINGLETON_H__INCLUDED
    
    #if (_MSC_VER > 1000)
    #pragma once
    #endif
    

    Nur mal eine Verständnisfrage: Reicht nicht eins von beiden, machen nicht beide das gleiche (bin mir nicht sicher)?



  • Hatten wir schon ein paar mal 😉 Aber egal ... ne machen nicht genau das selbe ... #pragma once verhindert sogar das der die Datei überhaupt noch anguckt ... aber such mal danach im FOrum ... findest de ne genauere Beschreibung zu ...



  • (D)Evil schrieb:

    Hatten wir schon ein paar mal 😉 Aber egal ... ne machen nicht genau das selbe ... #pragma once verhindert sogar das der die Datei überhaupt noch anguckt ... aber such mal danach im FOrum ... findest de ne genauere Beschreibung zu ...

    sicher?
    wikipedia



  • Airdamn schrieb:

    (D)Evil schrieb:

    Hatten wir schon ein paar mal 😉 Aber egal ... ne machen nicht genau das selbe ... #pragma once verhindert sogar das der die Datei überhaupt noch anguckt ... aber such mal danach im FOrum ... findest de ne genauere Beschreibung zu ...

    sicher?
    wikipedia

    Wikipedia schrieb:

    Using #pragma once instead of include guards will increase compilation speed on many implementations, because it is a higher-level mechanism; the compiler itself can compare filenames or inodes without having to invoke the C preprocessor to scan the header for #ifndef and #endif.

    Der Compiler darf es zumindest 😉


Anmelden zum Antworten