【问题标题】:Having difficulty passing initialization list for constructor for parent and child objects难以为父对象和子对象的构造函数传递初始化列表
【发布时间】:2015-03-13 21:51:35
【问题描述】:

我必须创建具有高度+宽度或半径的构造函数,以及 x、y、z 坐标的三个新可选参数,默认值为零。

所有四个构造函数(圆形值和引用、矩形值和引用)都必须有一个初始化列表,该列表调用 3 参数 Shape 构造函数。

如果 x,y,z 是传入的构造函数参数,请将它们传递给 Shape。

对于复制构造函数,使用传入矩形或圆形参数的 x,y,z。

我在“:Shape(.......)”中尝试了各种各样的东西,但没有运气。谁能指出我正确的方向?

形状.h

class Shape {
private:
// disallow copy constructor
Shape(const Shape &) {}
static int numinstances;
int myid;


protected:

const int getId() const { return myid; }
double posx, posy, posz;

public:

Shape(const double inx, const double iny, const double inz) {
    myid = numinstances;
    ++numinstances;
    posx = inx;
    posy = iny;
    posz = inz;
}
// destructor must be public to be used without overriding
~Shape() { --numinstances; }

const double getX() const { return posx; }
const double getY() const { return posy; }
const double getZ() const { return posz; }
}

矩形.h

class Rectangle : public Shape {
private:
double dimx, dimy;


public:
Rectangle(double indimx, double indimy) : Shape (0, 0, 0)
{
    setWidth(indimx);
    setHeight(indimy);
}

Rectangle(const Rectangle& inrect) : Shape (inrect.getX(), inrect.getY(),     inrect.getZ())
{
    setWidth(inrect.getWidth());
    setHeight(inrect.getHeight());
}

const double getWidth() const { return dimx; }
const double getHeight() const { return dimy; }

圆.h

const double PI = 3.14159;

class Circle : public Shape {
private:
double radius;
double x, y, z;

public:
Circle(double inrad) : Shape (0, 0, 0)
{
    setRadius(inrad);
}

Circle(const Circle& incirc) : Shape (incirc.getX(), incirc.getY(), incirc.getZ())
{
    setRadius(incirc.getRadius());
}

const double getRadius() const { return radius; }
void setRadius(double inrad) {
    radius = (inrad < 0 ? (0 - inrad) : inrad);
}

Main.cpp

Rectangle r1(2,3);
Circle c1(4);

Shape * shapeset[NUMSHAPES];
Rectangle * rectset[NUMRECT];
Circle * circset[NUMCIRC];

// populate arrays with generic shapes
for(i=0;i<NUMSHAPES;++i) {
    shapeset[i] = new Shape(1,2,3);
}
for(i=0;i<NUMRECT;++i) {
    rectset[i] = new Rectangle(i, i+1);
}
for(i=0;i<NUMCIRC;++i) {
    circset[i] = new Circle(i);
}

Shape * shapeset[NUMSHAPES];
Rectangle * rectset[NUMRECT];
Circle * circset[NUMCIRC];

// populate arrays with generic shapes
for(i=0;i<NUMSHAPES;++i) {
    shapeset[i] = new Shape(1,2,3);
}
for(i=0;i<NUMRECT;++i) {
    rectset[i] = new Rectangle(i, i+1, 1,2,3);
}
for(i=0;i<NUMCIRC;++i) {
    circset[i] = new Circle(i, 1,2,3);
}

错误代码

||=== Build: Debug in Project (compiler: GNU GCC Compiler) ===|
C:\X\main.cpp||In function 'int main()':|
C:\X\main.cpp|52|error: no matching function for call to 'Rectangle::Rectangle(int&, int, int, int, int)'|
C:\X\main.cpp|52|note: candidates are:|
C:\X\Rectangle.h|29|note: Rectangle::Rectangle(const Rectangle&)|
C:\X\Rectangle.h|29|note:   candidate expects 1 argument, 5 provided|
C:\X\Rectangle.h|23|note: Rectangle::Rectangle(double, double)|
C:\X\Rectangle.h|23|note:   candidate expects 2 arguments, 5 provided|
C:\X\main.cpp|55|error: no matching function for call to 'Circle::Circle(int&, int, int, int)'|
C:\X\main.cpp|55|note: candidates are:|
C:\X\Circle.h|30|note: Circle::Circle(const Circle&)|
C:\X\Circle.h|30|note:   candidate expects 1 argument, 4 provided|
C:\X\Circle.h|25|note: Circle::Circle(double)|
C:\X\Circle.h|25|note:   candidate expects 1 argument, 4 provided|
C:\X\main.cpp|13|warning: unused variable 'ARSIZE' [-Wunused-variable]|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|

感谢您的帮助!我真的在这里泡菜。

【问题讨论】:

  • 那么,您对这些编译器消息有什么特别不了解的地方?
  • 我不知道如何为 Child : Parent 构造函数提供正确数量的参数。

标签: c++ constructor parent initializer


【解决方案1】:

你需要将参数添加到子构造函数中,并将它们传递给父构造函数:

Rectangle(double indimx, double indimy, double x=0, double y=0, double z=0) : Shape (x, y, z)
{
    setWidth(indimx);
    setHeight(indimy);
}

double x=0x 设置默认值,如果它没有在调用中传递。

【讨论】:

  • 太好了,谢谢,成功了!这是有道理的,只是我自己无法弄清楚。
【解决方案2】:

这里有几个问题:

(1)C:\X\main.cpp|52|error: no matching function for call to 'Rectangle::Rectangle(int&amp;, int, int, int, int)'

这是说它找不到接受 5 个参数的矩形构造函数。您在Rectangle.h 中提供的(Rectangle(double indimx, double indimy))只需要 2 个参数。

(2)C:\X\main.cpp|55|error: no matching function for call to 'Circle::Circle(int&amp;, int, int, int)'

与 (1) 相同的问题。您将 4 个参数传递给 Circle 的构造函数,该构造函数只需要两个 (Circle(double inrad))。

尝试修复调用RectangleCircle 构造函数的方式(您只需要传入两个参数——矩形的宽度和高度以及圆的半径),看看是否可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2012-03-05
    • 2013-05-23
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多