简体   繁体   中英

I keep getting an error when I use this code. I'm trying to use constructors in an inherited class. c++

So, my problem is I don't really know a lot about classes. So, I am trying to get this constructor to work. I need the base constructor and the constructor of the derived class to work without implementing it there. I can define it there i just can't implement it. The compiler is telling me it's expecting a curly brace. #ifdef SHAPE.H #endif SHAPE.H #define

#include<string>
using namespace std;
class QuizShape
{
    private:    
        char outer, inner;
        string quizLabel;

    public:
        //Constructor
        QuizShape();
};

class Rectangle : public QuizShape
{
    public:
        int height, width;

        //Getter & setter methods
        int getHeight() const;
        void setHeight(int);
        int getWidth() const;
        void setWidth(int);

        //Constructor for Rectangle
        Rectangle() : QuizShape();
};

class Square : public Rectangle
{
    public:
        //constructors
        Square() : Rectangle (); This area here is where the error comes // IT says it expects a { but I'm not allowed to define the constructor in line.
        Square(int w, int h) : Rectangle (height , width);
}; 

class doubleSquare : public Square
{
//Fill in with constructors 
};

I don't understand the error it's giving me. I'm pretty sure I'm not redefining it either.

The constructor needs to be defined. Pls observe the changes in the way constructors are defined/used.

    #include<string>
    using namespace std;
    class QuizShape
    {
        private:    
            char outer, inner;
            string quizLabel;

        public:
            //Constructor
            QuizShape();
    };

    class Rectangle : public QuizShape
    {
        public:
            int height, width;

            //Getter & setter methods
            int getHeight() const;
            void setHeight(int);
            int getWidth() const;
            void setWidth(int);

            //Constructor for Rectangle
            Rectangle() { } 
            Rectangle(int h, int w): height(h), width(w) { }
    };

    class Square : public Rectangle
    {
        public:
            //constructors
          Square() { }  // 
          Square(int w, int h) : Rectangle (h, w) {}
    }; 

    class doubleSquare : public Square
    {
    //Fill in with constructors 
    };

Move your constructor initializer lists to the definitions. For example, for Square :

//declarations
Square();
Square(int w, int h);

//definitions
Square() : Rectangle() {/*body*/}
Square(int w, int h) : Rectangle(w, h) {/*body*/} //assuming you meant w, h

Do that for the other constructors with initializer lists in the declarations as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM