0

I have two files. One a header file named shape.h. I wanted to call the shapes from the class through the header file. But the file is not compiling.

#include <iostream>
#include <graphics.h>
using namespace std;

class Shape{
    protected:
        int x;
        int y;
    public:
        Shape(){
            x =0;
            y = 0;
            int gd = DETECT, gm;
            char pathtodriver[] = "";
            initgraph(&gd, &gm, pathtodriver);
        }
        virtual void draw();
};

class Rectangle : public Shape{
    protected:
        int x1;
        int y1;

    public:

        Rectangle(int x, int y,int x1,int y1){
            this->x = x;
            this->y = y;
            this->x1 = x1;
            this->y1 = y1;
        };

        void draw(){
            rectangle(200,200,100,250);
            closegraph();
        };
};


class Circle : public Shape{
    protected:
        int x1;

    public:

        Circle(int x,int y,int x1){
            this->x = x;
            this->y = y;
            this->x1 = x1;
        };
        void draw(){
            circle(x,y,x1);
            closegraph();
        };
};

class Ellipse : public Shape{
    protected:
        int a;
        int b;
        int x1;
        int y1;

    public:

        Ellipse(int x,int y,int a, int b,int x1,int y1){
            this->x = x;
            this->y = y;
            this->a = a;
            this->b = b;
            this->x1 = x1;
            this->y1 = y1;
        };
        void draw(){
            ellipse(x,y,a,b,x1,y1);
            closegraph();
        };
};

And another file to call in shapes.

#include "shape.h"
using namespace std;

int main(){
    Shape *s1;
    cout<<"1.Rectangle"<<
    "2.Circle"<<
    "3.Ellipse"<<
    "4.Exit"<<endl;
    int choice;
    cout<<"Enter your choice :"<<endl;
    cin >> choice;
    switch(choice){
    case 1:
        Rectangle R1(100,100,50,60);
        s1 = &R1;
        s1->draw();
        break;

    case 2:
        Circle c1(100,100,20);
        s1 = &c1;
        s1 ->draw();
        break;

    case 3:
        Ellipse e1(100,100,0,360,30,40);
        s1 = &e1;
        s1->draw();
        break;

    case 4:
        exit(0);

    default:
        cout<<"Error choice";
    }

}



But it gives the following errors:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\shape.h||In constructor 'Shape::Shape()':|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\shape.h|14|warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp||In function 'int main()':|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|13|error: expected ';' before 'R1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|14|error: 'R1' was not declared in this scope|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|20|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note:   crosses initialization of 'Circle c1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|21|error: expected ';' before 'e1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|22|error: 'e1' was not declared in this scope|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|24|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note:   crosses initialization of 'Circle c1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|27|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note:   crosses initialization of 'Circle c1'|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Please help

I tried changing the name of class but it did not work as well. The code did not compile.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Rb Mohit
  • 13
  • 3
  • 1
    Please include the errors as text in the question and indicate where in the above code they refer to. – Richard Critten Nov 27 '22 at 12:55
  • There's quite a lot wrong with this code, but I'm struggling to see how it causes the errors mentioned. – john Nov 27 '22 at 12:56
  • You need to put the `case`s statements in `{` ... `} break;` blocks. You also need to initialize `choice`. – Ted Lyngmo Nov 27 '22 at 12:58
  • The break statement does not affect the compiling of the code – Rb Mohit Nov 27 '22 at 12:59
  • protected data is almost always a bad idea – Neil Butterworth Nov 27 '22 at 13:00
  • The first error is about (stone age) `initgraph` taking a `char*` parameter, but a string literal decays to a `const char*`. So no match. Most of the rest then follows from Shape not compiling properly. – BoP Nov 27 '22 at 13:02
  • You do realize the `s1` variable is kind of useless? The specific shapes go out of scope when the `switch` is done, so `s1` becomes a dangling pointer at that time making any dereferencing Undefined Behaviour. You could simply use e.g. `c1.draw()`. – fabian Nov 27 '22 at 13:12
  • Yes I realize that. This is my class assignment, which specified to use a pointer. But even though I remove the s1 pointer, it will still not compile. – Rb Mohit Nov 27 '22 at 13:15

1 Answers1

2

You need to but the case statements with declarations in { ... } blocks. You also need to add breaks to not fallthrough to the next case statement. You also need to take the choice input from the user. You currently use choice uninitialized.

Example:

while (std::cin >> choice) {
    switch (choice) {
        case 1: {
            Rectangle R1(100, 100, 50, 60);
            s1 = &R1;
            s1->draw();
        } break;

        case 2: {
            Circle c1(100, 100, 20);
            s1 = &c1;
            s1->draw();
        } break;

        case 3: {
            Ellipse e1(100, 100, 0, 360, 30, 40);
            s1 = &e1;
            s1->draw();
        } break;

        case 4:
            std::exit(0);

        default:
            cout << "Error choice";
    }
}

Also note that void initgraph(int *graphdriver, int *graphmode, char *pathtodriver); takes a char* as the last argument. You provide a const char*. You need to change that:

char pathtodriver[] = "";
initgraph(&gd, &gm, pathtodriver);

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108