compilier errors



  • Hi 🙂
    Ich bekomme beim Compilieren im Codeblocks gleich 50 errors, obwohl ich nur ein total kleines Progrämmchen habe. Und seltsamerweise zu einer Datei, von der ich überhaupt nichts weiß 😞

    main.cpp:

    #include <iostream>
    #include <cstdlib>
    #include "Automat.h"
    
    using namespace std;
    int main(int argc, char *argv[])
    {
       Automat autom;
    
       return EXIT_SUCCESS;
    }
    

    Automat.h (liegt im gleichen Verzeichnis wie main.cpp):

    #ifndef AUTOMAT_H
    #define AUTOMAT_H
    
    class Automat
    {
        public:
            Automat();
            virtual ~Automat();
        protected:
        private:
    };
    
    #endif // AUTOMAT_H
    

    Automat.cpp (ebenfalls im gleichen Verzeichnis wie main.cpp):

    #include "Automat.h"
    
    Automat::Automat()
    {
        //ctor
    }
    
    Automat::~Automat()
    {
        //dtor
    }
    

    Beim Anlegen der Klasse Automat habe ich mich ganz auf CodeBlocks verlassen. Das kann selbst das Grundgerüst für Klassen anlegen.

    Aber jetzt bekomm ich uuuuunmengen an errors. Seltsamerweise zu einer Datei locale.h 😕

    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\250' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '`' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\6' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\24' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\20' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\376' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\5' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\340' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\310' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\31' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\34' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\6' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\1' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\1' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\7' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\10' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\250' in program|
    c:\programme\codeblocks\mingw\include\locale.h|1|error: stray '\32' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\360' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\200' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\20' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\330' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\20' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\360' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\330' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\20' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\4' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\200' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\1' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\4' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\1' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\6' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\330' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    c:\programme\codeblocks\mingw\include\locale.h|2|error: stray '\377' in program|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 24 second(s)) ===|

    Woher kommt denn dieses locale.h? Und diese Nummern sollen für Fehler stehen??



  • OMG

    Ich glaube, CodeBlocks ist für die Tonne.



  • Jetzt habe ich CodeBlocks deinstalliert, neu heruntergeladen und neu installiert. Die oben gezeigten Fehlermeldungen waren dann weg, funktioniert hat es trotzdem nicht.

    Danach habe ich versucht, ein fertiges Programm zu kompilieren. Ein Programm zum Buch "Grundkurs C++" (gibts aus dem Internet zu laden). Das Programm schaut folgendermaßen aus:

    main.cpp:

    // listings/006/automat1/main.cpp
    #include <iostream>
    #include <vector>
    #include <string>
    #include <memory>   // C++11
    #include "automat.h"
    using namespace std;
    
    int main(void) {
      unique_ptr<Automat> device_ptr(new Automat); // C++11
      vector <Automat> Vdevice(2);
      Automat device;
      // Initialisieren
      device_ptr->init(10000, "Bonn, Hauptstrasse 123");
    
      Vdevice[0].init(20000, "Augsburg, Fuggerweg 345");
      Vdevice[1].set_geld(25000);
      Vdevice[1].set_standort("Berlin, Berliner Allee 567");
    
      device.init(123456, "Frankfurt, Goethestrasse 123");
    
      // Ausgabe
      cout << device_ptr->get_geld() << endl;
      cout << device_ptr->get_standort() << endl;
    
      for(size_t i=0; i<Vdevice.size(); i++) {
        cout << Vdevice[i].get_geld() << endl;
        cout << Vdevice[i].get_standort() << endl;
      }
    
      cout << device.get_geld() << endl;
      cout << device.get_standort() << endl;
      return 0;
    }
    

    automat.h:

    // listings/006/automat.h
    #ifndef _AUTOMAT_H_
    #define _AUTOMAT_H_
    #include <string>
    using namespace std;
    
    class Automat {
      private:
        // Daten der Klasse "Automat"
        unsigned long geld{0UL}; // C++11
        string standort{"Unbekannt"};  // C++11
      public:
        // Elementfunktionen der Klasse "Automat"
        unsigned long get_geld();
        void set_geld(unsigned long g);
        string& get_standort();
        void set_standort(string s);
        void init(unsigned long g, string s);
    };
    #endif
    

    automat.cpp:

    #include <string>
    #include "automat.h"
    using namespace std;
    
    unsigned long Automat::get_geld() {
      return geld;
    }
    
    void Automat::set_geld(unsigned long g){
      geld = g;
    }
    
    string& Automat::get_standort() {
      return standort;
    }
    
    void Automat::set_standort(string s){
      standort = s;
    }
    
    void Automat::init(unsigned long g=0, string s="") {
      set_geld(g);
      set_standort(s);
    }
    

    Jetzt hat CodeBlocks Probleme mit dem Erkennen von Methoden:

    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to Automat::init(unsigned long, std::string)'| C:\\cpp\\beispiele\\2294\_listings\\1547\_listings\\006\\automat1\\C++11\\main.o:main.cpp|| undefined reference toAutomat::init(unsigned long, std::string)'|
    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to Automat::set_geld(unsigned long)'| C:\\cpp\\beispiele\\2294\_listings\\1547\_listings\\006\\automat1\\C++11\\main.o:main.cpp|| undefined reference toAutomat::set_standort(std::string)'|
    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to Automat::init(unsigned long, std::string)'| C:\\cpp\\beispiele\\2294\_listings\\1547\_listings\\006\\automat1\\C++11\\main.o:main.cpp|| undefined reference toAutomat::get_geld()'|
    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to Automat::get_standort()'| C:\\cpp\\beispiele\\2294\_listings\\1547\_listings\\006\\automat1\\C++11\\main.o:main.cpp|| undefined reference toAutomat::get_geld()'|
    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to Automat::get_standort()'| C:\\cpp\\beispiele\\2294\_listings\\1547\_listings\\006\\automat1\\C++11\\main.o:main.cpp|| undefined reference toAutomat::get_geld()'|
    C:\cpp\beispiele\2294_listings\1547_listings\006\automat1\C++11\main.o:main.cpp|| undefined reference to `Automat::get_standort()'|
    ||=== Build failed: 11 error(s), 0 warning(s) (0 minute(s), 22 second(s)) ===|

    Habt ihr eine Idee dazu, was hier schief läuft?

    Muss ich dazu noch irgendetwas im CodeBlocks konfigurieren? Oder müss ich im CodeBlocks ein Projekt dazu anlegen? Momentan sind das nämlich 3 ganz normale Dateien, die irgendwo auf der Festplatte liegen (allerdings alle in einem gemeinsamen Ordner).



  • Du musst automat.cpp zum Projekt hinzuzufügen, dann wird das von Code::Blocks auch kompiliert und gelinkt.

    Und ich hoffe, das using namespace im Header und die Defaultargumente in der .cpp-Datei hast du dazu gebaut und waren nicht schon im Original vorhanden.



  • Und ich hoffe, das using namespace im Header und die Defaultargumente in der .cpp-Datei hast du dazu gebaut und waren nicht schon im Original vorhanden.
    

    Habe nichts am Code geändert.

    Juhu. Hat geklappt 🙂

    das using namespace im Header

    Kommentiere ich im automat.h die Zeile 5 aus, bekomme ich jedoch wieder einen Fehler in der Zeile 11 angezeigt:

    ||=== Build: Debug in p1 (compiler: GNU GCC Compiler) ===|
    include\automat.h|11|error: 'string' does not name a type|
    include\automat.h|16|error: 'string' does not name a type|
    include\automat.h|17|error: 'string' has not been declared|
    include\automat.h|18|error: 'string' has not been declared|
    C:\cpp\p1\main.cpp||In function 'int main()':|
    C:\cpp\p1\main.cpp|14|error: invalid conversion from 'const char*' to 'int' [-fpermissive]|
    include\automat.h|18|error: initializing argument 2 of 'void Automat::init(long unsigned int, int)' [-fpermissive]|
    C:\cpp\p1\main.cpp|16|error: invalid conversion from 'const char*' to 'int' [-fpermissive]|
    include\automat.h|18|error: initializing argument 2 of 'void Automat::init(long unsigned int, int)' [-fpermissive]|
    C:\cpp\p1\main.cpp|18|error: invalid conversion from 'const char*' to 'int' [-fpermissive]|
    include\automat.h|17|error: initializing argument 1 of 'void Automat::set_standort(int)' [-fpermissive]|
    C:\cpp\p1\main.cpp|20|error: invalid conversion from 'const char*' to 'int' [-fpermissive]|
    include\automat.h|18|error: initializing argument 2 of 'void Automat::init(long unsigned int, int)' [-fpermissive]|
    C:\cpp\p1\main.cpp|24|error: 'class Automat' has no member named 'get_standort'|
    C:\cpp\p1\main.cpp|28|error: '__gnu_cxx::__alloc_traits<std::allocator<Automat> >::value_type' has no member named 'get_standort'|
    C:\cpp\p1\main.cpp|32|error: 'class Automat' has no member named 'get_standort'|
    ||=== Build failed: 15 error(s), 0 warning(s) (0 minute(s), 22 second(s)) ===|

    Scheint also benötigt zu werden.

    Defaultargumente in der .cpp-Datei

    Meinst du die Zeile 21 in automat.cpp?

    void Automat::init(unsigned long g=0, string s="") {
    

    Was stört dich denn daran?



  • tröröö schrieb:

    Ein Programm zum Buch "Grundkurs C++"

    Das ist von Jürgen Wolf.

    Mach um alles wo 'Jürgen Wolf' draufsteht einen großen Bogen!



  • tröröö schrieb:

    das using namespace im Header

    Kommentiere ich im automat.h die Zeile 5 aus, bekomme ich jedoch wieder einen Fehler in der Zeile 11 angezeigt:
    [...]
    Scheint also benötigt zu werden.

    Du musst das kompensieren, indem du alle Namen aus dem std-Namespace mit dem Präfix std:: versiehst. string --> std::string. Nur im Header natürlich.

    Der Grund ist, dass das Einbinden eines Headers, der einen Namenspace öffnet, in eine Quelldatei dazu führt, dass der Namespace auch in der Quelldatei geöffnet ist, ob man das will oder nicht. Deshalb die Regel: Kein using namespace in Headern.

    Achja, und besorg dir beizeiten ein besseres Buch.



  • Bashar schrieb:

    Achja, und besorg dir beizeiten ein besseres Buch.

    Streich das beizeiten. Im Prinzip sofort neues besorgen!



  • Der Grund ist, dass das Einbinden eines Headers, der einen Namenspace öffnet, in eine Quelldatei dazu führt, dass der Namespace auch in der Quelldatei geöffnet ist, ob man das will oder nicht. Deshalb die Regel: Kein using namespace in Headern.

    Wenn es die gleichen Namespaces sind, sollte das doch kein Problem geben, oder? Könntest mir bitte ein Bsp zeigen, wo das Programm dann einen Fehler liefert?

    Streich das beizeiten. Im Prinzip sofort neues besorgen!

    Kennst du zufällig ein kostengünstiges, gutes Buch für Anfänger?



  • tröröö schrieb:

    Ich glaube, CodeBlocks ist für die Tonne.

    Ganz im Gegenteil. Ich halte den von Microsoft fürn Anfang für ein wenig angenehmer, ausführlichere (deutsche) Fehlermeldungen (C1234 markieren und F1 drücken). Danach kommt gleich CodeBlocks.

    Aber CodeBlocks kann nix dafür, wenn Du die c:\programme\codeblocks\mingw\include\locale.h kaputtmachst.



  • Aber CodeBlocks kann nix dafür, wenn Du die c:\programme\codeblocks\mingw\include\locale.h kaputtmachst.

    Ich weiß noch nicht mal, was dieses locale.h ist. Und hab auch keine Ahnung, wie ich es hätte kaputt machen können.


Anmelden zum Antworten