Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

24.05.2017, 19:19

"C++" Fehler/ Probleme durch namespace

Hi,

ich verusche gerade einige Klassen in namespaces zu legen und bekomme dadurch Compilererrors (VS2017 Commuinity)


Error C2039 'logging': is not a member of 'spe' (compiling source file c_logging.cpp)
Error C3083 'logging': the symbol to the left of a '::' must be a type (compiling source file c_logging.cpp)
Error C2039 'C_Logging': is not a member of 'spe' (compiling source file c_logging.cpp)
Error C2143 syntax error: missing ';' before '*' (compiling source file c_logging.cpp)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file c_logging.cpp)
Error C2238 unexpected token(s) preceding ';' (compiling source file c_logging.cpp)
Error C2039 'timeing': is not a member of 'spe' (compiling source file c_logging.cpp)
Error C3083 'timeing': the symbol to the left of a '::' must be a type (compiling source file c_logging.cpp)
Error C2039 'C_Timer': is not a member of 'spe' (compiling source file c_logging.cpp)
Error C2143 syntax error: missing ';' before '*' (compiling source file c_logging.cpp)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file c_logging.cpp)
Error C2238 unexpected token(s) preceding ';' (compiling source file c_logging.cpp)
Error C2039 'logging': is not a member of 'spe' (compiling source file main.cpp)
Error C3083 'logging': the symbol to the left of a '::' must be a type (compiling source file main.cpp)
Error C2039 'C_Logging': is not a member of 'spe' (compiling source file main.cpp)
Error C2143 syntax error: missing ';' before '*' (compiling source file main.cpp)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file main.cpp)
Error C2238 unexpected token(s) preceding ';' (compiling source file main.cpp)
Error C2039 'timeing': is not a member of 'spe' (compiling source file main.cpp)
Error C3083 'timeing': the symbol to the left of a '::' must be a type (compiling source file main.cpp)
Error C2039 'C_Timer': is not a member of 'spe' (compiling source file main.cpp)
Error C2143 syntax error: missing ';' before '*' (compiling source file main.cpp)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (compiling source file main.cpp)
Error C2238 unexpected token(s) preceding ';' (compiling source file main.cpp)

und hier noch der Code

engine_core.h

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once

#include "main.hpp"

namespace spe
{
    namespace core
    {
        class engine_core
        {
            public:
                engine_core();
                ~engine_core();

            private:
                spe::logging::C_Logging *log;// = nullptr;
                spe::timeing::C_Timer   *running_timer;// = nullptr;

        };

        engine_core::engine_core()
        {
            //std::shared_ptr<C_Logging> log(new C_Logging());              // start logging
            //std::shared_ptr<C_Timer>  running_timer(new C_Timer());           // start timer
            //running_timer->start();
        }

        engine_core::~engine_core()
        {
        }
    }
}


c_timer.h


C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once

#include "main.hpp"

namespace spe
{
    namespace timeing
    {
        class C_Timer
        {
        public:
            C_Timer() {};
            virtual ~C_Timer() {};
            void    start()     // start timer
            {
                starttime = std::chrono::high_resolution_clock::now();
            };
            int stop()              // stoptimer and return of millisec since last start
            {
                stoptime = std::chrono::high_resolution_clock::now();
                std::chrono::high_resolution_clock::duration diff = stoptime - starttime;
                return std::chrono::duration_cast<std::chrono::milliseconds>(diff).count();
            };
            int restart()       // restart timer ans returns millisec since last call
            {
                stoptime = std::chrono::high_resolution_clock::now();
                std::chrono::high_resolution_clock::duration diff = stoptime - starttime;
                starttime = stoptime;
                //starttime = std::chrono::high_resolution_clock::now();
                return std::chrono::duration_cast<std::chrono::milliseconds>(diff).count();
            }


        private:
            std::chrono::high_resolution_clock::time_point starttime;
            std::chrono::high_resolution_clock::time_point stoptime;
        };
    }
}


c_logging.hpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

#include "main.hpp"


//class spe::C_Logging;
namespace spe
{
    namespace logging
    {
        class C_Logging
        {
        public:
            C_Logging();
            ~C_Logging();
            void operator<< (std::string data) const;
            void operator<< (char *) const;
        private:
            std::ofstream   *file;
            std::string     filename = "engine.log";
        };
    }
}


c_logging.cpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "c_logging.hpp"

namespace spe
{
    namespace logging {

        C_Logging::C_Logging()
            : filename(filename)
        {
            file = new std::ofstream();
            //file->open(filename);
            if (file->is_open())
                std::cerr.rdbuf(file->rdbuf());
        }

        C_Logging::~C_Logging()
        {
            if (file->is_open())
                file->close();
            file = nullptr;
        }

        void C_Logging::operator<<(std::string text) const
        {
            std::cerr << __DATE__ << " " << __TIME__ << " " << text << std::endl;
        }

        void C_Logging::operator<<(char *text) const
        {
            std::cerr << __DATE__ << " " << __TIME__ << " " << text << std::endl;
        }

    }
}


main.hpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once

/*

    File:   main.hpp

    Desc:   main inlude file

*/

#include <iostream>
#include <fstream>
#include <string>
#include <memory>
#include <chrono>


#include "engine_core.h"
#include "c_logging.hpp"
#include "c_timer.hpp"


main.cpp

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*

    File: main.cpp

    Desc: mainfile

*/

#include "main.hpp"

int main(int argv, char *argc)
{
    std::unique_ptr<spe::core::engine_core> engine(new spe::core::engine_core());

    

    char a;
    std::cin >> a;
}


Bitte um hilfe

mfg

sIR_pAPILEIN

2

24.05.2017, 19:53

In engine_core.h inkludierst du main.hpp, worin du wiederum engine_core.h inkludierst.
Ich würde an deines Stelle in main.hpp die benötigten STLs inkludieren (iostream, fstream, ...) und die mit den einzelnen Dateien der Engine eine Hierarchie aufbauen, die dafür sorgt, dass sie sich nicht gegenseitig inkludieren.

Nach folgenden Änderungen sollte es funktionieren:






3

24.05.2017, 20:05

thx jetzt gehts.

Sollte das gegenseitige includieren nicht durch include-guards verhintert werden?

4

31.05.2017, 09:53

Sollte das gegenseitige includieren nicht durch include-guards verhintert werden?

Das ist ja gerade das Problem. Wenn a.h b.h inkludiert und b.h a.h und a.cpp a.h, dann wird beim Kompilieren von b.h a.h ignoriert und folglich kennt der Compiler nicht die Deklarationen, die in a.h stehen.
Cube Universe
Entdecke fremde Welten auf deiner epischen Reise durchs Universum.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

5

31.05.2017, 12:35

Zirkuläre Abhängigkeiten sind evil. Das gilt nicht nur für Header, sondern auch generell unter Klassen. Typisches Beispiel: Spieler-Klasse nutzt Engine-Klasse, um auf das Spielfeld zuzugreifen, Spielfeld nutzt die Engine und greift auf Spieler zu und am Ende ist jeder von jedem abhängig, da jeder jeden kennt. Das ist kein guter Ansatz.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

Werbeanzeige