0

I'm trying to declare an object of type rectangle but it keeps showing me this error

this is the rectangle class which inherits from shape class:

class rectangle: public shape{

int width, height;
public:
    rectangle();

 void draw(int width, int height){
    for(int i = 1; i <= height; i++){
            for(int j = 1; j <= width; j++){
                if( i == 1 && i == height)
                    cout<<"*";
                else if( j == 1 && j == width)
                    cout<<"*";
                else
                    cout<<" ";
            }
            cout<<endl;
        }
    }


};

this is the shape class:

class shape
{
int x, y;
char letter;
public:
    shape(int x, int y, char letter, int length){
        int X = x;
        int Y = y;
        int L = letter;
    }
    void verLine(){
        for(int i = 0; i < 5; i++){
            cout<<"*"<<endl;
        }
    }
    void horLine(){
        for(int i = 0; i < 5; i++){
            cout<<"*";
        }
    }
    char setLetter(char letter){
        int newLetter = letter;
        return newLetter;
    }
    void gotoxy(int x, int y){
        HANDLE hConsole;
        COORD cursorLoe;
        std::cout.flush();
        cursorLoe.X = x;
        cursorLoe.Y = y;
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(hConsole, cursorLoe);
    }
};

and this is the main:

 int main()
 {
     rectangle r1;
     cout<<endl;
     r1.draw(5,5);
     return 0;
 }

I think the problem is with the default constructor I have in the rectangle class, but I din't find any other way to code it

  • 2
    You don't seem to have defined the default constructor for rectangle. –  Jun 07 '17 at 19:47

1 Answers1

1

You declared the default constructor but didn't define it. You need to define the function rectangle::rectangle() somewhere. If you don't need the constructor to actually do anything, then you don't need to declare it at all. So either define the constructor or don't declare it.

Blue Goose
  • 137
  • 5
  • when I deleted it I got this error: use of deleted function 'rectangle::rectangle()' – Haïthem Saoudi Jun 07 '17 at 19:49
  • @HaïthemSaoudi That's because `shape` doesn't have a default Constructor. Either give `shape` a default constructor, or explicitly define `rectangle()` to call `shape(int, int, char, int)` in its member initialization list. – Xirema Jun 07 '17 at 19:52
  • you need have an initializer list http://en.cppreference.com/w/cpp/language/initializer_list – Manvir Jun 07 '17 at 19:53
  • @Tyger I just declared a default constructor for shape but i still get the error undefined reference for shape::shape() – Haïthem Saoudi Jun 07 '17 at 20:02
  • @HaïthemSaoudi It worked for me [here](http://cpp.sh/8smrz) – Manvir Jun 07 '17 at 20:08
  • @HaïthemSaoudi did it work or are you still getting an error? – Manvir Jun 07 '17 at 20:13