Anzeige:
Ergebnis 1 bis 2 von 2

Thema: [C++]SIGSEGV-Problem

  1. #1
    Registrierter Benutzer
    Registriert seit
    07.01.2012
    Beiträge
    13

    [C++]SIGSEGV-Problem

    Hallo, ich verstehe nicht, was an meinem Code falsch ist.

    Code:
        #include <iostream>
    
        #include <string>
        #include <fstream>
    
        typedef bool (*BoolFnct) (void);
        BoolFnct NULLf_b=NULL;
    /* Class to handle a game
    */
    class cEngine
    {
        private:
            BoolFnct loop_mod; //! function pointer to game content 
            void handleEvents(); //! handle input and eevents
            void loop(); //! loop of the engine
            std::ofstream *enginelog; //! log file
        public:
            cEngine(BoolFnct lp);
            void addLogLine(const std::string txt="-1");
    };
    
    
    cEngine::cEngine(BoolFnct lp):
        loop_mod(NULLf_b),
        enginelog(NULL)
    {
        enginelog=new std::ofstream("log");
        addLogLine("Init Window ...");
        // ...
        addLogLine("Load Textures ...");
        // ...
        loop_mod=lp;
        loop();
    }
    
    void cEngine::handleEvents()
    {
    	// if key ESC was pressed exiting the application
    }
    
    void cEngine::loop()
    {
        addLogLine("Starting loop!");
        while (true)//(AppIsOpen())
        {
            addLogLine("looping ...");
            handleEvents();
            if(loop_mod==NULLf_b)
            {
                std::cout << "Game loop is not defined!" << std::endl;
                return;
            }
    
            if(loop_mod()==false) // if it's in End-State leave loop
            {
                addLogLine("shuting down ...");
                //App->close();
                break;
            }
        }
        addLogLine("Leaving loop ...");
    }
    
    void cEngine::addLogLine(const std::string txt="-1")
    {
        if(txt!="-1")
        {
            *enginelog << txt << std::endl;
            std::cout  << txt << std::endl;
        }
        else
        {
            *enginelog << std::endl;
            std::cout  << std::endl;
        }
    }
    
    cEngine *Eng;
    
    bool GameLoop();
    int main()
    {
        Eng = new cEngine(GameLoop);
        Eng->addLogLine("Sucessfull tested.");
        return true;
    }
    
    
    bool GameLoop()
    {
        std::cout << "[I]It will follow an Error." << std::endl;
        Eng->addLogLine("This is a test."); //! hier kommt der Fehler
        std::cout << "[I]Ist the error still there?" << std::endl;
    }
    Was läuft hier mit dem Funktionszeiger falsch?

  2. #2
    Administrator Avatar von anda_skoa
    Registriert seit
    17.11.2001
    Ort
    Graz, Österreich
    Beiträge
    5.477
    Du rufst im Konstruktor von cEngine loop() auf, was wiederum zum Aufruf von GameLoop führt.

    GameLoop ruft dann Eng->addLogLine auf, aber zu diesem Zeitpunkt ist Eng noch nicht initialisiert (das Programm exekutiert ja noch den Konstruktor von cEngine).

    Ciao,
    _
    Qt/KDE Entwickler
    Debian Benutzer

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •