Форум программистов, компьютерный форум, киберфорум
С++ для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
0 / 0 / 0
Регистрация: 02.10.2020
Сообщений: 88
1

Нет подходящего конструктора

09.11.2021, 10:40. Показов 354. Ответов 0
Метки c++, oop (Все метки)

Author24 — интернет-сервис помощи студентам
Вся программа
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include "iostream"
#include "conio.h"
#include "string"
 
using namespace std;
 
class Rastenie {
protected:
    string    type;
    string name;
    int    code;
    float S;
public:
    Rastenie() {
        type = "";
            code = 0;
        name = "";
        S = 0;
 
    }
 
    Rastenie(string type, string name, float S) {
        
            this->type = type;
    
        this->name = name;
        this->S = S;
    }
 
    Rastenie(Rastenie * rastenie) {}
    Rastenie GetCopy() {
    
        return this;
    }
 
    //virtual Rastenie* GetCopy() = 0;
 
    //Геттеры
    string GetType() {
        return type;
    }
    string GetName() {
        return name;
    }
    float GetS() {
        return S;
    }
    int GetCode() {
        return code;
    }
 
    //Сеттеры
    void SetType(string type) {
            this->type = type;
    }
    void SetName(string name) {
        this->name = name;
    }
    void SetS(float S) {
        this->S = S;
    }void SetCode(int code) {
        this->code = code;
    }
 
    // заполнить объект данными с клавиатуры
    virtual void Fill() {
        this->type = type;
 
        cout << "Введите имя: ";
        cin >> this->name;
 
        cout << "Введите площадь: ";
        cin >> this->S;
 
    }
    //Вывести информацию в консоль
    virtual void Show() {};
 
};
 
//класс "часть"
class Derevo : public Rastenie {
private:
    float h; // Высота растения 
 
public:
    //Конструктор без параметров
    Derevo() : Rastenie() {}
 
    //Конструктор с параметрами
    Derevo(string type, string name,float S,float h) : Rastenie(type, name,S) {
        this->h = h;
    }
    Derevo(Derevo* q): Rastenie(type, name, S) {
        this->SetH(h);
        this->SetName(name);
        this->SetType(type);
        this->SetS(S);
        this->SetH(h);
    }
    // Возвращает указатель на копию данного объекта
    
    //Сеттеры
    void SetH(float h) {
        this->h = h;
    }
 
    //Геттеры
    float GetH() {
        return h;
    }
 
    // заполнить объект данными с клавиатуры
    void Fill() {
        Rastenie::Fill();
 
        cout << "Введите высоту растения: ";
        cin >> this->h;
 
        cout << endl;
        //Derevo(type, name, S, h);
 
 
 
    }
    /*Derevo* GetCopy() {
        //Derevo* derevo = new Derevo;
        this->SetName(name);
        this->SetType(type);
        this->SetS(S);
        this->SetH(h);
 
 
        return this;
    }*/
    void Show() {
        cout << "Derevo - " << name << ",\t высота - " << h  << ",\t площадь - " << S << endl;
    }
};
 
//класс "часть"
class Trava : public Rastenie {
private:
    float h; // 
 
public:
    //Конструктор без параметров
    Trava() : Rastenie() {}
 
    //Конструктор с параметрами
    Trava(string type, string name, float S, float h) : Rastenie(type, name, S) {
        this->h = h;
    }
        Trava(Rastenie rest) : Rastenie(type, name, S) {
        //this->h = h;
    }
 
    //Сеттеры
    void SetH(float h) {
        this->h = h;
    }
 
    //Геттеры
    float GetH() {
        return h;
    }
 
    // заполнить объект данными с клавиатуры
    void Fill() {
        Rastenie::Fill();
 
    //  cout << "Введите высоту растания: ";
    //  cin >> this->h;
 
        cout << endl;
    }
 
    void Show() {
        cout << "Trava - "<< name << ",\t высота - " << h <<",\t площадь - "  <<S<< endl;
    }
};
 
// класс "целое"
class Uchastoc {
private:
    Rastenie** rastenie; 
    int          max; // количество 
    int          count; // количество растений на участке
    int          code;  
    float S;
 
public:
    Uchastoc() { // конструктор по умолчанию
        int max;
        do {
            cout << "Введите количество растений на участке: "; 
            cin >> max;
            cout << endl;
        } while (max < 1);
 
        rastenie = new Rastenie* [max];
        this->max = max;
 
        count = 0;
        code = 0;
    }
    
    Uchastoc(int count, int max, int code, float S, int choice) { // конструктор с параметрами
        this->rastenie = new Rastenie* [max];
        if (choice == 1)
            for (int i = 0; i < count; i++)         
                this->rastenie[i] = new Derevo [max];
 
        else
            for (int i = 0; i < count; i++)
            this->rastenie[i] = new Trava [max];
 
        this->max = max;
        this->S = S;
        this->count = count;
        this->code = code;
    }
 
    //Конструктор копии
    Uchastoc(Uchastoc* shelf,int choice) {
        Rastenie* ras;
        this->rastenie = new Rastenie* [max];
        if(choice==1)
        for (int i = 0; i < shelf->count; i++)
            this->rastenie[i] = new Derevo;
        else
        for (int i = 0; i < shelf->count; i++)
            this->rastenie[i] = new Trava;
 
            this->rastenie[shelf->count] = new Rastenie (ras->GetCopy());
 
        this->max = shelf->max;
        this->S = shelf->S;
        this->count = shelf->count;
        this->code = shelf->code;
    }
 
    ~Uchastoc() {
        if (count > 0)
            for (int i = 0; i < max; i++)
                delete rastenie[i];
        delete[] rastenie;
    }
 
    bool Add(Rastenie* ras,int choice) { // высадить растение на участок 
        if (count + 1 > max) {
            cout << "Невозможно высадить растение. Нет места." << endl;
            return false;
        }
        else {
            if (choice == 1) {
                //Derevo* der;
            //  ras = new Derevo;
                //rastenie->Fill();
                this->rastenie[count] = new Derevo;// (der->GetCopy());
            }
            else
                this->rastenie[count] = new Trava;// (ras->GetCopy());
            count++;
            return true;
        }
    }
 
        
            
        
    bool Remove(int n) { // убрать литературу с полки
        return false;
        if (n > 0 && n <= count) {
            delete rastenie[n - 1];
            Rastenie* tmp = rastenie[n - 1];
 
            if (n < count)
                for (int i = n - 1; i < count - 1; i++)
                    rastenie[i] = rastenie[i + 1];
 
            rastenie[count - 1] = tmp;
 
            count--;
            return true;
        }
    
    }
 
    void Show() { 
        if (count > 0)
            for (int i = 0; i < count; i++) {
                cout << "#" << i + 1 << ": ";
                rastenie[i]->Show();
            }
        else cout << "На этом участке нет растнеий." << endl;
    }
 
    Rastenie** GetRasten() {
        return rastenie;
    }
 
    int GetCount() { // количество растений на участке
        return count;
    }
 
    void SetCode(int code) {
        this->code = code;
    }
 
    int GetCode() {
        return code;
    }
};
 
// Класс-обработчик
class Visadka {
private:
    Uchastoc** rast; 
    int     max;   // количество участков
    float S;
 
public:
    Visadka() {
        int max;
        float S=0;
        do {
            cout << "Введите количество учасков: "; 
            cin >> max;
            cout << endl;
        } while (max < 1);
 
        rast = new Uchastoc * [max];
        for (int i = 0; i < max; i++) rast[i] = NULL;
 
        this->max = max;
        this->S = S;
    }
 
    Visadka(Uchastoc** rastenie, int count, int max,int choice) {
        this->rast = new Uchastoc * [max];
        for (int i = 0; i < count; i++)
            this->rast[i] = new Uchastoc(rastenie[i], choice);
        this->max = max;
        this->S += S;
 
    }
 
    ~Visadka() {
        for (int i = 0; i < max; i++)
            if (rast[i] != NULL)
                delete rast[i];
        delete[] rast;
    }
 
    bool Add(Rastenie* rastenie, int N, int choice) {
        N--;
        if (N >= 0 && N <= this->max) {
            if (rast[N] == NULL) {
                rast[N] = new Uchastoc();
                rast[N]->SetCode(rastenie->GetCode());
            }
            else if (rast[N]->GetCode() != rastenie->GetCode()) {
                return false;
            }
 
            return rast[N]->Add(rastenie, choice);
        }
        else cout << "Неправильный номер участка." << endl;
        return false;
    }
 
    bool Remove(int nomUchastka, int n) { 
        nomUchastka--;
 
        if (nomUchastka < 0 || nomUchastka >= max) {
            cout << "Неправильный номер." << endl;
            return false;
        }
 
        if (rast[nomUchastka] == NULL) {
            cout << "Участок пуст" << endl;
            return false;
        }
 
        return rast[nomUchastka]->Remove(n);
    }
 
    void Search(string name) {
        cout << "Найти растение по названию: " << name << endl;
        Rastenie** rastenie;
        for (int i = 0; i < max; i++) {
            if (rast[i] != NULL) {
                rastenie = rast[i]->GetRasten();
                for (int j = 0; j < rast[i]->GetCount(); j++) {
                    if (rastenie[j]->GetName() == name) {
                        rastenie[j]->Show();
                        return;
                    }
                }
            }
        }
 
        cout << "Растение не найдено" << endl;
    }
 
    // Показывает все 
    void Show() {
        if (max > 0) {
            for (int i = 0; i < max; i++) {
                if (rast[i] != NULL) {
                    cout << "Растение на участке - " << i + 1 << endl;
                    rast[i]->Show();
                }
                else cout << "На участке - " << i + 1 <<" - нет растений." << endl;
            }
        }
        else cout << "Нет участков." << endl;
    }
 
    int GetMax() { // количество растений, которое высажено
        return max;
    } 
};
 
int main() {
    
    setlocale(LC_CTYPE, "Russian");
    Rastenie * rastenie = nullptr;
    Visadka razmeshen = Visadka();
 
    bool result;
    int choice, nom, n;
    string name;
        
    while (true) {
        cout << "Меню: " << endl
            << "1 - Вывести все" << endl
            << "2 - Добавить растение" << endl
            << "3 - Удалить растение" << endl
            << "4 - Найти растение" << endl
            << "5 - Выход" << endl
            << "Введите номер: ";
        cin >> choice;
        cout << endl;
 
        switch (choice) {
        case 1:
            razmeshen.Show();
            cout << endl;
            break;
 
        case 2:
            //int uc; //показывпает, что у нас выбрано 0-деревоб 1- трава
            do {
                cout << "Выберите тип растения: " << endl
                    << "1 - Дерево" << endl
                    << "2 - Трава" << endl
                    << "3 - Выход" << endl
                    << "Введите номер: ";
                cin >> choice;
            } while (choice != 1 && choice != 2 && choice != 3);
            cout << endl;
 
            if (choice == 3) {
                system("pause");
                return 0;
            }
            
            if (choice == 1) { 
                rastenie = new Derevo;
                rastenie->Fill();
            }
            else if (choice == 2) {
                rastenie = new Trava;
                rastenie->Fill();
            }
            
            cout << "Введите номер участка: ";
            cin >> nom;
            cout << endl;
 
            result = razmeshen.Add(rastenie, nom, choice);
 
            if (result == true) cout << "Растение добавлено" << endl << endl;
            else if (result == false) cout << "Нет места. Ошибка." << endl << endl;
 
            delete rastenie;
 
            break;
        
        case 3:
            cout << "Введите номер участка: ";
            cin >> nom;
            cout << "Введите номер растения: ";
            cin >> n;
            cout << endl;
 
            result = razmeshen.Remove(nom, n);
 
            if (result == true) cout << "Растеное удалено" << endl << endl;
            else if (result == false) cout << "Ошибка." << endl << endl;
 
            break;
 
        case 4:
            cout << "Введите название растения: ";
            cin >> name;
            cout << endl;
 
            razmeshen.Search(name);
            cout << endl;
            break;
 
        case 5:
            system("pause");
            return 0;
            break;
        }
    }
}
Add ,но одна проблема:
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool Add(Rastenie* ras,int choice) { // высадить растение на участок 
        if (count + 1 > max) {
            cout << "Невозможно высадить растение. Нет места." << endl;
            return false;
        }
        else {
            if (choice == 1) {
                //Derevo* der;
            //  ras = new Derevo;
                //rastenie->Fill();
                this->rastenie[count] = new Derevo;// (der->GetCopy());
            }
            else
                this->rastenie[count] = new Trava;// (ras->GetCopy());
            count++;
            return true;
        }
    }[
Нет подходящего конструктора. Я не имею представление, какие параметры в него передавать
C++
1
this->rastenie[count] = new Trava(ras->GetCopy());
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
09.11.2021, 10:40
Ответы с готовыми решениями:

Нет подходящего конструктора по умолчанию
написал вот такой код // ConsoleApplication29.cpp: определяет точку входа для консольного...

Нет подходящего конструктора по умолчанию
Всем привет :) Есть вот такой код#include &lt;vector&gt; #include &lt;SFML/Graphics.hpp&gt; using namespace...

Нет подходящего конструктора по умолчанию
Не могу взять в толк, почему ругается на отсутствие конструктора по умолчанию, ведь я их прописал в...

Нет подходящего конструктора по умолчанию
#ifndef ANIMATION_H #define ANIMATION_H #include &lt;SFML\Graphics.hpp&gt; class animation {...

Ошибка: нет подходящего конструктора по умолчанию
В общем ребят, такая проблема, решил начать обучение плюсов по книжке, там задание создать класс и...

0
09.11.2021, 10:40
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
09.11.2021, 10:40
Помогаю со студенческими работами здесь

Нет подходящего конструктора vector по умолчанию
Не понимаю, как описать #include &quot;stdafx.h&quot; #include &lt;iostream&gt; #include &lt;vector&gt; using...

Нет подходящего конструктора по умолчанию. Наследование
Скидываю хидер и исходник к нему: хидер(Main_Menu.h): #include &quot;stdafx.h&quot; #ifndef...

C2512: odejda: нет подходящего конструктора по умолчанию
Пытаюсь разобраться с полиморфизмом. Помогите пожалуйтста. #include &lt;string&gt; #include...

Создание лог файла. Нет подходящего конструктора по умолчанию
Добрый день, почему вылетает ошибка не пойму. Ошибка: нет подходящего конструктора по умолчанию. И...

Не удаётся добавить в вектор объектов экземпляры класса (нет подходящего конструктора по умолчанию)
Здравствуйте! Столкнулся со следующей проблемой. Мне нужно создать вектор, который содержит...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
1
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru