3 C:\Dev-Cpp\Unbenannt2.cpp `cout' undeclared (first use this function)



  • Hi

    Habe mit C++ angefangen und bin dabei mit dem Bjarne Stroustroup-Buch zu lernen.
    Allerdings habe ich Probleme mit dem kompilieren einfachster Programme...

    Habe folgenden Quellcode aus dem Buch einfach mal "abgeschrieben":

    bool accept()
    {
         cout << "Moechten Sie weitermachen (j oder n)?\n"; // stelle Frage
    
         char antwort = 0;
         cin >> antwort;
         if (antwort == `j`) return true;
         return false;
    }
    bool accept2()
    {
         cout << "Möchten Sie weitermachen (j oder n)?\n";
    
         char antwort = 0;
         cin >> antwort;
    
         switch (antwort) {
                case `j`:
                     return true;
                case `n`:
                     return false;
                default:
    cout << "Ich halte das fuer ein Nein.\n";
    return false;
    }
    }
    bool accept3()
    {
         int versuche = 1;
         while (versuche < 4) {
               cout << "Moechten Sie weitermachen (j oder n)?\n";
               char antwort = 0;
               cin >> antwort;
    
               switch (antwort) {
                      case `j`:
                           return true
                      case `n`:
                           return false;
                      default:
                              cout << "Ich verstehe Sie leider nicht.\n";
                              versuche = versuche + 1;
                              }
                              }
                              cout << "Ich halte das für ein Nein.\n";
                              return false;
                              }
    

    und bekomme beim kompilieren folgende Fehlermeldung:

    C:\Dev-Cpp\Unbenannt2.cpp In function bool accept()': 3 C:\\Dev-Cpp\\Unbenannt2.cppcout' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    6 C:\Dev-Cpp\Unbenannt2.cpp cin' undeclared (first use this function) 7 C:\\Dev-Cpp\\Unbenannt2.cpp stray '' in program
    7 C:\Dev-Cpp\Unbenannt2.cpp stray '' in program 7 C:\\Dev-Cpp\\Unbenannt2.cppj' undeclared (first use this function)
    C:\Dev-Cpp\Unbenannt2.cpp In function bool accept2()': 12 C:\\Dev-Cpp\\Unbenannt2.cppcout' undeclared (first use this function)
    15 C:\Dev-Cpp\Unbenannt2.cpp cin' undeclared (first use this function) 18 C:\\Dev-Cpp\\Unbenannt2.cpp stray '' in program
    18 C:\Dev-Cpp\Unbenannt2.cpp stray '' in program 18 C:\\Dev-Cpp\\Unbenannt2.cppj' undeclared (first use this function)
    20 C:\Dev-Cpp\Unbenannt2.cpp stray '' in program 20 C:\\Dev-Cpp\\Unbenannt2.cpp stray '' in program
    20 C:\Dev-Cpp\Unbenannt2.cpp n' undeclared (first use this function) C:\\Dev-Cpp\\Unbenannt2.cpp In functionbool accept3()':
    31 C:\Dev-Cpp\Unbenannt2.cpp cout' undeclared (first use this function) 33 C:\\Dev-Cpp\\Unbenannt2.cppcin' undeclared (first use this function)
    36 C:\Dev-Cpp\Unbenannt2.cpp stray '' in program 36 C:\\Dev-Cpp\\Unbenannt2.cpp stray '' in program
    36 C:\Dev-Cpp\Unbenannt2.cpp j' undeclared (first use this function) 38 C:\\Dev-Cpp\\Unbenannt2.cpp expected;' before "case"
    38 C:\Dev-Cpp\Unbenannt2.cpp stray '' in program 38 C:\\Dev-Cpp\\Unbenannt2.cpp stray '' in program
    38 C:\Dev-Cpp\Unbenannt2.cpp `n' undeclared (first use this function)

    Woran kann das liegen??
    Benutze Dev-C++ von Bloodsheed



  • Abtippen alleine reicht nicht. Man muss schon wissen, was man da so macht. 😉

    Schau mal hier:
    http://www.henkessoft.de/C++/C++/cpp_konsole.htm
    http://www.henkessoft.de/C++/C++ Fortgeschrittene/C++_Fortgeschrittene.htm#1.1._Tutorials_zum_Einstieg_in_C

    Versuche mal das hier:

    #include <iostream>
    
    void wait() //ausführliche Version
    {
        std::cin.clear();
        std::streambuf* pbuf = std::cin.rdbuf();
        std::streamsize size = pbuf->in_avail();
        std::cin.ignore(size);
        std::cin.get();
    }
    
    int main()
    {
        std::cout << "C++ ist nix fuer Abtipper" << std::endl;    
        wait();   //verhindert "Schließen der Konsole unter MS Windows"
        return 0; //nicht unbedingt notwendig
    }
    

    Viel Spaß!



  • C:\Dev-Cpp\Unbenannt2.cpp In function bool accept()': 3 C:\\Dev-Cpp\\Unbenannt2.cppcout' undeclared (first use this function)

    Der Compiler meint (wie man hier ablesen kann), dass der Bezeichner (Identifier) cout nicht aufgelöst werden kann. Er weiss nicht, was er mit cout anfangen soll. Er vermutet zwar, dass es eine Variable oder ein Objekt sein könnte, aber er kann nirgends eine Deklaration finden. Damit hat er Recht, denn cout befindet sich in der iostream-Bibliothek, die du nicht eingebunden hast. Deshalb oben im Code

    #include <iostream>
    using namespace std;
    

    einfügen oder noch besser, das using namespace std; weglassen und vor jedes cin / cout ein std:: schreiben (also std::cin / std::cout ).

    [edit]
    Erhard Henkes war schneller.
    [/edit]



  • Der Compiler meint ... weiss nicht, was er ... anfangen soll. Er vermutet zwar, ..., aber ... Damit hat er Recht, ...

    Einen Compiler sollte man nicht zu sehr "vermenschlichen", auch wenn er von Menschen hergestellt wurde. 😃



  • Zu denken wie ein Compiler hilft bei der Fehlersuche ungemein.


Anmelden zum Antworten