当类具有多个默认构造函数C++时声明对象数组
Declare array of objects when the class has more then one default constructor C++
你好,我有class point
,工作是制作array of points
(同一类的对象),但该类有多个构造函数。如何声明我想要在数组中加入哪一个?代码:
class point
{
private:
double pointX, pointY;
string color;
int form;
public:
point();
point(double, double, string color = "red", int form = 2);
point(string color = "red", int form = 2);
~point() {
cout << "Deleting object point" << endl;
}
inline void print();
inline void distance();
};
point::point(double x, double y, string color = "red", int form = 2) {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
}
point::point(string color = "red", int form = 2) {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
}
point::point() {
cout << "enter x coordinate of the point x = "; cin >> pointX;
cout << "enter y coordinate of the point y = "; cin >> pointY;
cout << "enter color of the point "; getline(cin, color);
cout << "enter number form 1 - 3 for the form of the point "; cin >> form;
}
inline void point::print() {
cout << "the x coordinate of the point is x = " << pointX << endl;
cout << "the y coordiante of the point is y = " << pointY << endl;
cout << "the color of the point is " << color << endl;
if (form = 1) cout << "the form is circle" << endl;
if (form = 2) cout << "the form is square" << endl;
if (form = 2) cout << "the form is cross" << endl;
}
inline void point::distance() {
double z;
z = sqrt(pointX*pointX + pointY*pointY);
cout << "distance between the point and the start of coordinate system is " << z << endl;
}
double pointDistatnce() {
double z, x, y;
point points = new point[4];
}
!!! point points = new point[4];` // here must be the array of objects but it shows me error that "class point has more than one default constructor"?
我想使用没有参数的构造函数,以便用户构建自己的点。以下是错误列表中的错误;
!! no suitable constructor exists to convert from "point
!! class "point" has more than one default constructor
point();
声明一个默认构造函数。 point(string color = "red", int form = 2);
要么去掉第一个,只使用第二个,要么去掉第二个color
的默认值。
根据您发布的额外信息进行编辑:您正在寻找编译器来阅读程序员的想法。 您定义了一个没有参数的构造函数来执行一项操作,但您定义了一个具有两个执行不同操作的可选参数的构造函数。 当调用构造函数并且没有给出任何参数时,这是否意味着它应该在没有参数的情况下执行,或者是否意味着它应该使用两个可选参数的默认值执行。
这些都没有改变我原来的答案。 这只是意味着您需要思考一下才能使用该答案。 在您最初提供的部分代码中,人们可能希望每个构造函数都可以在没有参数的情况下调用,彼此是冗余的,因此只需删除冗余即可修复它。 但是,由于您希望程序员能够调用两个不同的构造函数之一,因此您需要想一种方法来告诉编译器应该使用哪一个。
在重新考虑设计时,您还应该尝试放弃在构造函数中使用cin
的想法。 这在技术上并没有错。 但这是一个足够糟糕的主意,你不应该这样做。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别