Singleton mit DLL, fetter Streit.



  • Hi,

    hier mal der ganze Code davon 🙂 ist zum glück net viel 😃 Exportiert wird, wie man sieht (also denk ich mal :D)

    // Dateiname:		EagleX.h
    // //////////////////////////////////////////////////////////////////////////////////
    // Autor:			Patrick Ullmann                    (c) by Xarent Interactive 2003
    //
    // Erstelldatum:	22.07.2003
    // letzte Änderung:	22.07.2003	(Patrick)	Datei erstellt.
    // //////////////////////////////////////////////////////////////////////////////////
    // Info:			Header der EagleX Engine
    // //////////////////////////////////////////////////////////////////////////////////
    
    // M A K R O S //////////////////////////////////////////////////////////////////////
    #ifdef EAGLEX_EXPORTS
    #define EAGLEX_API __declspec(dllexport)
    #else
    #define EAGLEX_API __declspec(dllimport)
    #endif
    
    // I N C L U D E S //////////////////////////////////////////////////////////////////
    #include <windows.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <string>
    #include <fstream>
    
    // I N C L U D E S  -  E A G L E _ X ////////////////////////////////////////////////
    #include "exLog.h"
    
    // Dateiname:		exLog.h
    // //////////////////////////////////////////////////////////////////////////////////
    // Autor:			Patrick Ullmann                    (c) by Xarent Interactive 2003
    //
    // Erstelldatum:	22.07.2003
    // letzte Änderung:	22.07.2003	(Patrick)	Datei erstellt.
    // //////////////////////////////////////////////////////////////////////////////////
    // Info:			Protokollstruktur
    // //////////////////////////////////////////////////////////////////////////////////
    
    #ifndef _EXLOG_H_
    #define _EXLOG_H_
    
    // I N C L U D E S //////////////////////////////////////////////////////////////////
    	// Noch keine
    
    // D E F I N I T I O N E N //////////////////////////////////////////////////////////
    	// Noch keine
    
    // S T R U K T U R E N //////////////////////////////////////////////////////////////
    class EAGLEX_API exLog 
    { 
    public: 
    	static exLog		 &getInstance	(void) { return (m_Instance); }
    
    	void setFile (const char *FileName = "Protokoll.txt");
    
    	exLog& operator<< (const char* s); 
    
    private: 
    	exLog (void); 
    
    	static	exLog	m_Instance;
    
    	std::ofstream	m_Stream; 
    	std::string		m_FileName; 
    }; 
    
    #endif
    
    // Dateiname:		exLog.cpp
    // //////////////////////////////////////////////////////////////////////////////////
    // Autor:			Patrick Ullmann                    (c) by Xarent Interactive 2003
    //
    // Erstelldatum:	22.07.2003
    // letzte Änderung:	22.07.2003	(Patrick)	Datei erstellt.
    // //////////////////////////////////////////////////////////////////////////////////
    // Info:			Protokollstruktur
    // //////////////////////////////////////////////////////////////////////////////////
    
    // I N C L U D E S //////////////////////////////////////////////////////////////////
    #include <EagleX.h>
    
    // D E F I N I T I O N E N //////////////////////////////////////////////////////////
    exLog exLog::m_Instance;
    
    // F U N K T I O N E N //////////////////////////////////////////////////////////////
    exLog::exLog(void) 
    { 
    } 
    
    void exLog::setFile (const char *FileName)
    {
    	this->m_FileName = FileName;
    	this->m_Stream.open (this->m_FileName.c_str(), std::ios::out); 
    
    	this->m_Stream << "Protokolldatei:\n\n";
    
    	this->m_Stream.close (); 
    }
    
    exLog& exLog::operator<< ( const char* s ) 
    {    
    	this->m_Stream.open (this->m_FileName.c_str(), std::ios::out | std::ios::app); 
    	this->m_Stream << s; 
    	this->m_Stream.close (); 
    
    	return (*this); 
    }
    

    also mehr ist das nicht 🙂

    und so sieht mein Testprogramm aus:

    #include <windows.h>
    #include <eaglex.h>
    #pragma comment(lib, "EagleX.lib")
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
    	exLog::getInstance().setFile ();
    	exLog::getInstance() << "Bla";
    
    	return 0;
    }
    

    Aber ich verzweifle richtig an dem Singleton, da ich nicht weis, was falsch sein könnte 😞



  • hmm, und wo bitte wird die variable m_Instance initialisiert ???

    eigentlich sieht so ein singletzon-construct so aus:

    // static attribute
    static myClass* myClass::m_Instance = NULL;
    
    // privat constructor
    myClass::myClass()
    {
    }
    
    // public getInstance
    myClass* myClass::getInstance()
    {
       if(m_Instance == NULL)
       {
          m_Instance = new myClass;
       }
    
       return m_Instance;
    }
    

    dat läuft auch in einer dll

    RockNix ///



  • Hi,

    hier wird intialisiert: exLog exLog::m_Instance;

    ist ja ein Referenzsingleton und kein Pointersingleton!´

    Ich muss den nehmen weil sonst funzt das mit dem operator<< nicht 😞 Ich hab bisher noch keine andere lösung dafür gefunden einen singleton zu benutzen und einen operator<< einzubauen 😞

    Drum muss ich den halt nehmen, oder es zeigt mir einer, einen wo das auch geht und der funzt 😃

    Denn den mit dem pointer und new, den kenn ich auch, aber da funzt kein operator<< 😞



  • Patrick schrieb:

    Ich muss den nehmen weil sonst funzt das mit dem operator<< nicht 😞

    Dann ändere das Singleton so ab:

    // static attribute 
    static myClass* myClass::m_Instance = NULL; 
    
    // privat constructor 
    myClass::myClass() 
    { 
    } 
    
    // public getInstance 
    myClass& myClass::getInstance() 
    { 
       if(m_Instance == NULL) 
       { 
          m_Instance = new myClass; 
       } 
    
       return *m_Instance; 
    }
    


  • Hi,

    danke für die schnelle hilfe, aber wie muss das im private mit m_Instance aussehen? also die deklaration.

    weil ich habe bei der getInstance(); derbste fehler

    edit:
    Achja, wie ruf ich den singleton dann auf? also ich kenn diesen singleton net so sehr 🙄



  • Ich versuche es mal in deinem Code:

    class EAGLEX_API exLog 
    { 
    public: 
    	static exLog		 &getInstance	(void) { return (*m_Instance); } // !
    
    	void setFile (const char *FileName = "Protokoll.txt");
    
    	exLog& operator<< (const char* s); 
    
    private: 
    	exLog (void); 
    
    	static	exLog*	m_Instance; // !
    
    	std::ofstream	m_Stream; 
    	std::string		m_FileName; 
    };
    
    // D E F I N I T I O N E N //////////////////////////////////////////////////////////
    exLog* exLog::m_Instance; // !
    
    // F U N K T I O N E N //////////////////////////////////////////////////////////////
    exLog::exLog(void) 
    { 
    } 
    
    void exLog::setFile (const char *FileName)
    {
    	this->m_FileName = FileName;
    	this->m_Stream.open (this->m_FileName.c_str(), std::ios::out); 
    
    	this->m_Stream << "Protokolldatei:\n\n";
    
    	this->m_Stream.close (); 
    }
    
    exLog& exLog::operator<< ( const char* s ) 
    {    
    	this->m_Stream.open (this->m_FileName.c_str(), std::ios::out | std::ios::app); 
    	this->m_Stream << s; 
    	this->m_Stream.close (); 
    
    	return (*this); 
    }
    


  • hi,

    der fehler kommt noch immer 😞



  • Der Compiler- oder der Prozedureinsprungspunkt-Fehler?



  • wieder der Prozedureinsprungspunkt-Fehler. Compilieren tut er fehlerfrei



  • *push*

    Sorry aber es ist echt wichtig. 😞



  • *push*
    Leute bitte, es ist echt wichtig! Einer muss doch was wissen, wie das geht 😞 😞 😞 😞



  • Nicht so aufdringlich!
    Wann genau kommt denn der Fehler und bei was? Evtl musst du deinen header mit "" anstelle von <> einbinden. Hast du etwas an den Projekteinstellungen verändert (Win32 Anwendung?)



  • wieso sollte ich <> durch "" ändern? ich hab die sourcedateien und includepfade ans projekt umgeleitet so wie sich das für DLLs normalerweise gehören? Funzt auch prima.

    Der fehler kommt genau dort, wo ich getInstance(); aufrufe. Es wurde nix geändert.



  • Hast du den Fehler schon gefunden? Wenn nicht, kannst du mir vielleicht das Ganze einschließlich der Projekt-Dateien schicken.


Anmelden zum Antworten