Problem mit code: Visual c++ geht, Dev-c++ aber nicht?!



  • Also, ich hab einen Code (hängt unten an), welcher sich nicht mit Dev-c++ Linken und Kompilieren, aber wenn ich ihn mit Visual C++ versuche zu kompilieren gibt es keine Warnung, geschweige denn Fehler.
    Das merkwürdige daran: Ich dachte, der Code wäre ISO-Standard.
    Lässt sich auch sehr gut mit einem smily ausdrücken: 😕
    Und noch was vornweg: Ich hab ganz neu mit C++ angefangen (meine erste Programmiersprache), also plz net killn, wenn ich ein paar furchtbare Fehler gemacht hab.
    Ich würde mich sehr freuen, wenn sich jemand die Mühe macht, mir zu helfen. 🙂

    Code:

    #include <iostream.h>
    #include <conio.h>
    enum CHOICE { DrawRect = 1, GetArea, GetPerim, ChangeDimensions, Quit};
    
    class Rectangle
    {
        public:
            Rectangle(int width, int height);
            ~Rectangle();
    
            int GetHeight() const { return itsHeight; }
            int GetWidth () const { return itsWidth; }
            int GetArea() const { return itsHeight*itsWidth; }
            int GetPerim() const { return 2*itsHeight + 2*itsWidth; }
            void SetSize(int newWidth, int nesHeight);
    
        private:
            int itsWidth;
            int itsHeight;
    };
    
    void Rectangle::SetSize(int newWidth, int newHeight)
    {
        itsWidth = newWidth;
        itsHeight = newHeight;
    }
    
    Rectangle::Rectangle(int width, int height)
    {
        itsWidth = width;
        itsHeight = height;
    }
    
    Rectangle::~Rectangle() {}
    
    int DoMenu();
    void DoDrawRect(Rectangle);
    void DoGetArea(Rectangle);
    void DoGetPerim(Rectangle);
    
    int main()
    {
        Rectangle Rect1(25,6);
    
        int Choice = DrawRect;
        int fQuit = false;
    
        while (!fQuit)
        {
            Choice = DoMenu();
            if (Choice < DrawRect || Choice > Quit)
            {
                cout <<"Wrong input. Try again.\n" <<endl;
                continue;
            }
            switch (Choice)
            {
                case DrawRect:
                    DoDrawRect(Rect1);
                    break;
                case GetArea:
                    DoGetArea(Rect1);
                    break;
                case GetPerim:
                    DoGetPerim(Rect1);
                    break;
                case ChangeDimensions:
                    int newWidth, newHeight;
                    cout <<"Input new width: ";
                    cin >>newWidth;
                    cout <<"Input new height: ";
                    cin >>newHeight;
                    Rect1.SetSize(newWidth, newHeight);
                    DoDrawRect(Rect1);
                    break;
                case Quit:
                    fQuit = true;
                    cout <<"\nExit...\n" <<endl;
                    break;
                default:
                    cout <<"\007Strange Error!\007" <<endl;
                    fQuit = true;
                    break;
            }
        }
        getch();
        return 0;
    }
    
    int DoMenu()
    {
        int Choice;
        cout <<"\n\n  __|Program Options|__  \n" <<endl;
        cout <<"(1) Draw rectangle" <<endl;
        cout <<"(2) Area" <<endl;
        cout <<"(3) Perim" <<endl;
        cout <<"(4) New Size" <<endl;
        cout <<"(5) Quit" <<endl;
    
        cin >> Choice;
        return Choice;
    }
    
    void DoDrawRect(Rectangle Rect1)
    {
        int height = Rect1.GetHeight();
        int width = Rect1.GetWidth();
    
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
                cout <<"#";
            cout <<"\n";
        }
    }
    
    void DoGetArea(Rectangle Rect1)
    {
        cout <<"Area: " <<Rect1.GetArea() <<endl;
    }
    
    void DoGetPerim(Rectangle Rect1)
    {
        cout <<"Perim: " <<Rect1.GetPerim() <<endl;
    } // Programm Ende
    

    Fehlermedlungen bei dev-c++:
    In file included from D:/Dev-Cpp/include/c++/backward/iostream.h:31,
    from D:/Dev-Cpp/ListingR1_1.cpp:1:
    D:/Dev-Cpp/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
    D:/Dev-Cpp/ListingR1_1.cpp:37: variable or field DoDrawRect' declared void D:/Dev-Cpp/ListingR1\_1.cpp:37: invalid conversion fromBOOL ()(HDC__, int,
    int, int, int)' to int' D:/Dev-Cpp/ListingR1_1.cpp:38: variable or fieldDoGetArea' declared void
    D:/Dev-Cpp/ListingR1_1.cpp:38: invalid conversion from BOOL (*)(HDC\_\_*, int, int, int, int)' toint'

    D:/Dev-Cpp/ListingR1_1.cpp:39: variable or field DoGetPerim' declared void D:/Dev-Cpp/ListingR1\_1.cpp:39: invalid conversion fromBOOL ()(HDC__, int,
    int, int, int)' to int' D:/Dev-Cpp/ListingR1_1.cpp: In functionint main()':
    D:/Dev-Cpp/ListingR1_1.cpp:43: `Rectangle' undeclared (first use this function)
    D:/Dev-Cpp/ListingR1_1.cpp:43: (Each undeclared identifier is reported only

    once for each function it appears in.)
    D:/Dev-Cpp/ListingR1_1.cpp:43: parse error before `(' token

    D:/Dev-Cpp/ListingR1_1.cpp:59: Rect1' undeclared (first use this function) D:/Dev-Cpp/ListingR1_1.cpp:59:DoDrawRect' cannot be used as a function
    D:/Dev-Cpp/ListingR1_1.cpp:62: `DoGetArea' cannot be used as a function

    D:/Dev-Cpp/ListingR1_1.cpp:65: `DoGetPerim' cannot be used as a function

    D:/Dev-Cpp/ListingR1_1.cpp:74: `DoDrawRect' cannot be used as a function

    D:/Dev-Cpp/ListingR1_1.cpp: At global scope:
    D:/Dev-Cpp/ListingR1_1.cpp:104: parse error before )' token D:/Dev-Cpp/ListingR1_1.cpp: In functionvoid DoDrawRect(...)':
    D:/Dev-Cpp/ListingR1_1.cpp:105: void DoDrawRect(...)' redeclared as different kind of symbol D:/Dev-Cpp/ListingR1_1.cpp:37: previous declaration ofint DoDrawRect'
    D:/Dev-Cpp/ListingR1_1.cpp:37: previous non-function declaration int DoDrawRect' D:/Dev-Cpp/ListingR1_1.cpp:105: conflicts with function declarationvoid
    DoDrawRect(...)'

    D:/Dev-Cpp/ListingR1_1.cpp: At global scope:
    D:/Dev-Cpp/ListingR1_1.cpp:117: parse error before )' token D:/Dev-Cpp/ListingR1_1.cpp: In functionvoid DoGetArea(...)':
    D:/Dev-Cpp/ListingR1_1.cpp:118: void DoGetArea(...)' redeclared as different kind of symbol D:/Dev-Cpp/ListingR1_1.cpp:38: previous declaration ofint DoGetArea'
    D:/Dev-Cpp/ListingR1_1.cpp:38: previous non-function declaration int DoGetArea ' D:/Dev-Cpp/ListingR1_1.cpp:118: conflicts with function declarationvoid
    DoGetArea(...)'

    D:/Dev-Cpp/ListingR1_1.cpp: At global scope:
    D:/Dev-Cpp/ListingR1_1.cpp:122: parse error before )' token D:/Dev-Cpp/ListingR1_1.cpp: In functionvoid DoGetPerim(...)':
    D:/Dev-Cpp/ListingR1_1.cpp:123: void DoGetPerim(...)' redeclared as different kind of symbol D:/Dev-Cpp/ListingR1_1.cpp:39: previous declaration ofint DoGetPerim'
    D:/Dev-Cpp/ListingR1_1.cpp:39: previous non-function declaration int DoGetPerim' D:/Dev-Cpp/ListingR1_1.cpp:123: conflicts with function declarationvoid
    DoGetPerim(...)'
    //whitespace ist leider draußen... irgendeine idee, wie man den mitkopieren kann?



  • 1. benutz [ cpp] tags
    2. lies dir die wirklich einmalig genauen fehlermeldungen durch
    3.vergiss das:#include <iostream.h>
    4. benutz das:

    #include <iostream>
    using namespace std;
    

    5. benenn ma die klasse rectangle um,ich glaub da gibts nen namenskonflikt mit dem compiler



  • Genial! Vielen Dank. So schnell eine Antwort, die auch noch weiterhilft, gibt's ja selten!
    Kein einziger Fehler mehr. Woran genau lag das denn jetzt?
    MfG, ein hocherfreuter Xabre 😉



  • es lag wohl an dem namenskonflikt,es ist das naheliegendste, wenn man sieht, was die funktionen in denen rectangle als parameter vorkommt eine völlig andre parameterliste haben als sonstwo im code definiert 😉

    wird wohl in irgendeiner header dateieine funktion namens rectangle geben, genaues weis ich nich, da ich den compiler nich kenne 😉

    die sache mit den headern ist aber die, dass wir jetzt in c++ schreiben, und da werden diese standard header nichmehr benutzt,es gibt nämlich jetzt den neuen namespace std...



  • Jo, diesen namespace std. (was immer er bedeuten mag) hab ich auch gleich 'eingebaut' und seitdem gibt es auch diese hässlichen Warnungen nicht mehr. Vielen dank nochmal.



  • namespaces sind ein neues werkzeug,um funktionen in logische bereiche aufzuteilen.

    namespaces lösen nähmlich solche namenskonflikte welche bei dir aufgetreten sind.

    bsp:

    //stark gekürzt
    namespace a{
        void blub(int){
        } 
    }
    namespace b{
        void blub(int){//bis auf den namespace die absolut gleiche funktion..
        }
    }
    //inder main funktion
    blub(1);//fehler
    a::blub(1);//kein fehler
    b::blub(1);//kein fehler
    using namespace a;//an alle funktionen dieses namespaces wird automatisch a:: angehängt
    blub(1);//es wird automatisch a::blub(1) aufgerufen
    using namespace b;
    blub(1)//fehler da beide namespaces aktiv sind,es ist keine eindeutigkeit mehr vorhanden
    


  • namespaces lösen nähmlich solche namenskonflikte welche bei dir aufgetreten sind.

    Aber nicht wenn man mit using namespace std; alles in den globablen Namesraum kippt 😕 .



  • der globale namensraum std ist nur für die standard c++ funktionen bzw die stl soweit ich weis


Anmelden zum Antworten