所以,我的问题是我对课程并不是很了解。所以,我试图让这个构造函数工作。我需要基础构造函数和派生类的构造函数,而无需在那里实现它。我可以在那里定义它我无法实现它。编译器告诉我它期待一个大括号。 #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
};
我不明白它给我的错误。我很确定我也没有重新定义它。
答案 0 :(得分: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
};
答案 1 :(得分: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
对于其他构造函数,也在声明中使用初始化程序列表。