Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 难以传递父对象和子对象的构造函数的初始化列表_C++_Constructor_Parent_Initializer - Fatal编程技术网

C++ 难以传递父对象和子对象的构造函数的初始化列表

C++ 难以传递父对象和子对象的构造函数的初始化列表,c++,constructor,parent,initializer,C++,Constructor,Parent,Initializer,我必须使构造函数具有高度+宽度或半径,以及默认值为零的x、y、z坐标的三个新可选参数 所有四个构造函数(圆值和引用、矩形值和引用)都必须有一个调用3参数形状构造函数的初始值设定项列表 如果x、y、z是传入的构造函数参数,则将它们传递给Shape 对于复制构造函数,使用传入矩形或圆参数的x、y、z 我试过“:Shape(……”)里面的各种东西,但我没有运气。谁能给我指出正确的方向吗 形状.h class Shape { private: // disallow copy constructor S

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

所有四个构造函数(圆值和引用、矩形值和引用)都必须有一个调用3参数形状构造函数的初始值设定项列表

如果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);
}

谢谢你的帮助!我在这里真的很为难。

您需要将参数添加到子构造函数并将其传递给父构造函数:

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

double x=0
设置调用中未传递的
x
的默认值。

这里有几个问题:

(1)
C:\X\main.cpp | 52 |错误:调用“矩形::矩形(int&,int,int,int,int,int)”时没有匹配函数

这意味着它找不到包含5个参数的矩形的构造函数。您在
Rectangle.h
中提供的(
Rectangle(double indimx,double indimy)
)只接受2个参数

(2)
C:\X\main.cpp | 55 |错误:调用“Circle::Circle(int&,int,int,int)”时没有匹配函数

与第(1)款相同。您正在将4个参数传递到只接受两个(
Circle(double-inrad)
)的Circle的构造函数中


尝试确定如何调用
矩形
构造函数(您只需要传入两个-矩形的宽度和高度以及圆的半径),然后看看这是否有效。

,关于这些编译器消息,您有什么特别不了解的?我不知道如何为Child:Parent构造函数提供适当数量的参数。太好了,谢谢,做到了!这是有道理的,只是我自己想不出来。
||=== 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)) ===|
Rectangle(double indimx, double indimy, double x=0, double y=0, double z=0) : Shape (x, y, z)
{
    setWidth(indimx);
    setHeight(indimy);
}