Verständnisfrage



  • Hallo,
    warum funktioniert folgendes:

    bla.h

    #ifndef BLA
    #define BLA
    
    namespace nbla {
    	class cbla {
    	public:
    		void fbla();
    	};
    };
    
    void nbla::cbla::fbla() { }
    
    #endif
    

    main.cpp

    #include <iostream>
    #include "bla.h"
    
    int main() {
    	nbla::cbla foo;
    	foo.fbla();
    	return 0;
    }
    

    Aber folgendes nicht? 😕
    bla.h

    #ifndef BLA
    #define BLA
    
    namespace nbla {
    	class cbla {
    	public:
    		void fbla();
    	};
    };
    
    #endif
    

    bla.cpp

    #include "bla.h"
    
    void nbla::cbla::fbla() { }
    

    main.cpp

    #include <iostream>
    #include "bla.cpp"
    
    int main() {
    	nbla::cbla foo;
    	foo.fbla();
    	return 0;
    }
    

    Mein Compiler meldet mir folgenden Fehler:

    1>bla.obj : error LNK2005: "public: void __thiscall nbla::cbla::fbla(void)" (?fbla@cbla@nbla@@QAEXXZ) ist bereits in main.obj definiert.
    1>C:\Users\Documents\Visual Studio 2005\Projects\blatest\Debug\blatest.exe : fatal error LNK1169: Mindestens ein mehrfach definiertes Symbol gefunden.
    

    Vielen Dank im Voraus! 🙂



  • .cpp Dateien inkludieren ist eher semi-smart;)



  • D schrieb:

    Hallo,
    warum funktioniert folgendes:

    bla.h

    #ifndef BLA
    #define BLA
    
    namespace nbla {
    	class cbla {
    	public:
    		void fbla();
    	};
    };
    
    void nbla::cbla::fbla() { }
    
    #endif
    

    main.cpp

    #include <iostream>
    #include "bla.h"
    
    int main() {
    	nbla::cbla foo;
    	foo.fbla();
    	return 0;
    }
    

    Aber folgendes nicht? 😕
    bla.h

    #ifndef BLA
    #define BLA
    
    namespace nbla {
    	class cbla {
    	public:
    		void fbla();
    	};
    };
    
    #endif
    

    bla.cpp

    #include "bla.h"
    
    void nbla::cbla::fbla() { }
    

    main.cpp

    #include <iostream>
    #include "bla.cpp"
    
    int main() {
    	nbla::cbla foo;
    	foo.fbla();
    	return 0;
    }
    

    Mein Compiler meldet mir folgenden Fehler:

    1>bla.obj : error LNK2005: "public: void __thiscall nbla::cbla::fbla(void)" (?fbla@cbla@nbla@@QAEXXZ) ist bereits in main.obj definiert.
    1>C:\Users\Documents\Visual Studio 2005\Projects\blatest\Debug\blatest.exe : fatal error LNK1169: Mindestens ein mehrfach definiertes Symbol gefunden.
    

    Vielen Dank im Voraus! 🙂

    wenn ich compiler wäre und das compilieren müsste, würde ich da auch streiken



  • Wie this->that sagte, man inkludiert keine *.cpp Dateien, inkludiere einfach den Header, also bla.h und dann funktioniert es...



  • bla.obj : error LNK2005: "public: void __thiscall nbla::cbla::fbla(void)" (?fbla@cbla@nbla@@QAEXXZ) ist bereits in main.obj definiert.

    Kann es sein, dass du die Datei "bla.cpp" bereits gelinkt hast? Somit wäre der Code für die Funktion "nbla::cbla::fbla()" bereits enthalten. Wenn du dann mit #include den Code nochmals einbindest, dann ist dieser doppelt. (siehe Fehlermeldung: ist bereits in main.obj definiert.)


Anmelden zum Antworten