Здравствуйте, у меня есть class point
, и задание должно сделать array of points
(объекты того же класса), но класс имеет более одного конструктора. Как объявить, какой из них я хочу в своем массиве? Код:Объявить массив объектов, когда класс имеет более одного конструктора по умолчанию C++
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
Как должен быть выбран компилятор между двумя конструкторами по умолчанию? – NathanOliver
Сделайте свой класс одним однозначным конструктором по умолчанию. Только вы знаете, что вы хотите делать, когда аргументы не приводятся. Сделайте * one * вещь, которая делает это. – chris
Класс не может иметь более одного конструктора по умолчанию, поэтому вопрос спорный. – juanchopanza