Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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 - Fatal编程技术网

C++ “如何解决问题”;重新定义默认参数;类内构造函数

C++ “如何解决问题”;重新定义默认参数;类内构造函数,c++,constructor,C++,Constructor,考虑以下示例: class Rectangle{ Rectangle(int x, int y, int width, int height); Rectangle(int topLeft_x, int topLeft_y, int bottomRight_x, int bottomRight_y); }; 一个矩形对象可以通过给定(x,y)坐标加上宽度和高度或者给定左上角点对和右下角点对来构建 虽然从面向对象的角度来看这是正确的,但从编译器的角度来看这不是正确的,返回

考虑以下示例:

class Rectangle{
    Rectangle(int x, int y, int width, int height);
    Rectangle(int topLeft_x, int topLeft_y, int bottomRight_x, int bottomRight_y);    
};
一个矩形对象可以通过给定(x,y)坐标加上宽度和高度或者给定左上角点对和右下角点对来构建

虽然从面向对象的角度来看这是正确的,但从编译器的角度来看这不是正确的,返回错误“成员函数已定义或声明”

虽然我通常在成员函数的情况下很容易解决这个问题,只需根据它的作用更改名称,但对于构造函数来说这是不可能的


解决这个问题的最简单和正确的方法是什么?保持构建对象的两种方式?

您自己已经写过了

左上配对,右下配对

因此,您需要定义class
Point
,并在构造函数声明中使用此类型

否则,构造函数声明为

class Rectangle{
    Rectangle(int, int, int, int);
    Rectangle(int, int, int, int);    
};
正如您所见,即使要编写多行注释,这些声明也没有意义。:)

另一种方法是声明第一个构造函数,如

class Rectangle{
    Rectangle(int x, int y, unsigned int width, unsigned int height);
    Rectangle(int topLeft_x, int topLeft_y, int bottomRight_x, int bottomRight_y);    
};
但是,这种方法是不安全的,因为指定为第三个或第四个参数的每个整数文字都必须强制转换

您可以使用标准类
std::pair
,而不是类
点。比如说

#include <utility>

//...

class Rectangle{
    Rectangle(int x, int y, int width, int height);
    Rectangle( const std::pair<int, int> &topLeft, const std::pair<int, int> &bottomRight);    
};
#包括
//...
类矩形{
矩形(整数x,整数y,整数宽度,整数高度);
矩形(常数标准::成对&左上角,常数标准::成对&右下角);
};

您已经自己编写了

左上配对,右下配对

因此,您需要定义class
Point
,并在构造函数声明中使用此类型

否则,构造函数声明为

class Rectangle{
    Rectangle(int, int, int, int);
    Rectangle(int, int, int, int);    
};
正如您所见,即使要编写多行注释,这些声明也没有意义。:)

另一种方法是声明第一个构造函数,如

class Rectangle{
    Rectangle(int x, int y, unsigned int width, unsigned int height);
    Rectangle(int topLeft_x, int topLeft_y, int bottomRight_x, int bottomRight_y);    
};
但是,这种方法是不安全的,因为指定为第三个或第四个参数的每个整数文字都必须强制转换

您可以使用标准类
std::pair
,而不是类
点。比如说

#include <utility>

//...

class Rectangle{
    Rectangle(int x, int y, int width, int height);
    Rectangle( const std::pair<int, int> &topLeft, const std::pair<int, int> &bottomRight);    
};
#包括
//...
类矩形{
矩形(整数x,整数y,整数宽度,整数高度);
矩形(常数标准::成对&左上角,常数标准::成对&右下角);
};
另一种可能的解决方案(不是@vladfrommosco建议的一对)是执行构造的静态方法。这使您可以为它们指定不同的名称,因为它们的参数列表非常相似。这就是所谓的

另一个可能的解决方案(除了@vladfrommosco建议的一对)是执行构造的静态方法。这使您可以为它们指定不同的名称,因为它们的参数列表非常相似。这就是所谓的


解决此问题的另一种方法是使用:

与其使用具有不同名称的方法,不如给它们一个新参数,例如

struct-usecarks{};
结构useDimension{};
类矩形
{
矩形(使用角点、int-topLeft、int-topRight、int-bottomLeft、int-bottomRight)
{ ...
}
矩形(useDimension、int-topLeft、int-topRight、int-width、int-height)
{ ... 
}

};解决此问题的另一种方法是使用:

与其使用具有不同名称的方法,不如给它们一个新参数,例如

struct-usecarks{};
结构useDimension{};
类矩形
{
矩形(使用角点、int-topLeft、int-topRight、int-bottomLeft、int-bottomRight)
{ ...
}
矩形(useDimension、int-topLeft、int-topRight、int-width、int-height)
{ ... 
}

};很好的解决方案。有没有其他方法不创建一个新类来管理它?我会使用“unsigned int”而不是“size\t”由于宽度和高度更好地描述了变量的范围,并且具有与int相同的大小。@ABCplus您可以使用标准类std::pairI部分同意更新的解决方案:如果您添加的构造函数采用右上角点对和左下角点对,那么您将再次陷入冲突。这是一个不错的解决方案。有没有其他方法不创建一个新类来管理它?我会使用“unsigned int”而不是“size\t”由于宽度和高度更好地描述了变量的范围,并且具有与int相同的大小。@ABCplus您可以使用标准类std::pairI部分同意更新的解决方案:如果您添加的构造函数采用右上角点对和左下角点对,您将再次陷入冲突。没有默认参数这里,编译器肯定在抱怨其他代码。这里的问题是这两个声明是相同的。更改参数的名称不会影响这一点。@PeteBecker:EDIT:error message correct此处没有默认参数,因此编译器肯定在抱怨其他代码。这里的问题是这两个声明是相同的。更改参数的名称不会影响这一点。@PeteBecker:EDIT:错误消息更正