1

所以,我的问题是我对课程知之甚少。所以,我试图让这个构造函数工作。我需要基构造函数和派生类的构造函数来工作,而无需在那里实现它。我可以在那里定义它,但我无法实现它。编译器告诉我它需要一个花括号。#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 
};

我不明白它给我的错误。我很确定我也没有重新定义它。

4

2 回答 2

1

需要定义构造函数。请观察构造函数定义/使用方式的变化。

    #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 
    };
于 2013-04-17T02:51:28.717 回答
0

将您的构造函数初始值设定项列表移动到定义中。例如,对于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

对声明中带有初始化列表的其他构造函数也这样做。

于 2013-04-17T02:47:08.123 回答