Search code examples
c++oopbgi

Expected ";" before Object_name C++


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.


Solution

  • 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