The essence of the problem is this: I need to push a class object onto the stack and then take it out from there, but after I take it out and pass it back from the stack to the object, the class fields are not filled with the values ​​of the object that was passed to the stack. How is this possible to implement?

Here is the code

#include "Pharmacy.h"//мой класс #include "Hospital.h"//мой класс #include <stack> using namespace std; int main() { stack<Pharmacy> stkPharmObj; //стак объектов класса string str; Pharmacy objPharmacy, objPharmacy1, objPharmacy2, objPharmacy3; //Внизу куча текста, для запроса и передачи значений. Знаю, делал через цикл, тоже не работало, но так я хоть уверен, что все ввел. Сорян за количество строк cout << "Input Town" << endl; cin >> str; cin.ignore(); objPharmacy.setTown(str); cout << "Input name" << endl; getline(cin, str); objPharmacy.setName(str); cout << "Input Street" << endl; getline(cin, str); objPharmacy.setStreet(str); cout << "Input NumberBuilding" << endl; getline(cin, str); objPharmacy.setNumberBuilding(str); stkPharmObj.push(objPharmacy);//Вот первый объект в стек cout << "Input Town" << endl; cin >> str; cin.ignore(); objPharmacy1.setTown(str); cout << "Input name" << endl; getline(cin, str); objPharmacy1.setName(str); cout << "Input Street" << endl; getline(cin, str); objPharmacy1.setStreet(str); cout << "Input NumberBuilding" << endl; getline(cin, str); objPharmacy1.setNumberBuilding(str); stkPharmObj.push(objPharmacy1);//Вот второй cout << "Input Town" << endl; cin >> str; cin.ignore(); objPharmacy2.setTown(str); cout << "Input name" << endl; getline(cin, str); objPharmacy2.setName(str); cout << "Input Street" << endl; getline(cin, str); objPharmacy2.setStreet(str); cout << "Input NumberBuilding" << endl; getline(cin, str); objPharmacy2.setNumberBuilding(str); stkPharmObj.push(objPharmacy2);//Вот третий cout << "Input Town" << endl; cin >> str; cin.ignore(); objPharmacy3.setTown(str); cout << "Input name" << endl; getline(cin, str); objPharmacy3.setName(str); cout << "Input Street" << endl; getline(cin, str); objPharmacy3.setStreet(str); cout << "Input NumberBuilding" << endl; getline(cin, str); objPharmacy3.setNumberBuilding(str); stkPharmObj.push(objPharmacy3);//Вот четвертый objPharmacy = stkPharmObj.top();//вот тут я пытаюсь объекту передать значение последнего объекта, должен в этом отображаться объект 3 //функции отображения. objPharmacy.Show();//здэс пустота, поля не пришли из объекта который я пытался достато из стека objPharmacy2.Show();//а здесь все отлична //Хотя стек принял все объекты норм и указал размер 4 system("pause"); } 

Below is the class code

  #pragma once #include "BasikZaklad.h" class Pharmacy : public BasikZaklad { private: string strWorkingKlas, strFormVlas; public: Pharmacy() {}; Pharmacy(const Pharmacy &obj) { setWorkingKlas(strWorkingKlas); setFormVlas(strFormVlas); }; ~Pharmacy() {}; virtual void Show() override { cout << "string strWorkingKlas ==" << getWorkingKlas() << endl; cout << "string strFormVlas ==" << getFormVlas() << endl; BasikZaklad::Show(); } void setWorkingKlas(string strWorkingKlas); void setFormVlas(string strFormVlas); string getWorkingKlas(void) { return strWorkingKlas; } string getFormVlas(void) { return strFormVlas; } }; void Pharmacy::setWorkingKlas(string strWorkingKlas) { try { char* chWorkingKlas{}; int iCountWorkingKlas = 0; if ((chWorkingKlas = (char*)calloc(40, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chWorkingKlas[i] = strWorkingKlas[i]; if (checkNumbers(strWorkingKlas)) throw chWorkingKlas; if (strWorkingKlas[i] == '\0') break; iCountWorkingKlas++; if (iCountWorkingKlas > 40) throw iCountWorkingKlas; } if (!islower(strWorkingKlas[0])) { this->strWorkingKlas = strWorkingKlas; } else throw strWorkingKlas; } catch (char* chWorkingKlas) { cout << "Error invalid WorkingKlas!!! Wright it without numbers or other chars" << endl; } catch (string strWorkingKlas) { cout << "Error invalid Town!!! Wright it from big letter" << endl; } catch (int iCountWorkingKlas) { cout << "Error invalid Town!!! Too much chars" << endl; } } void Pharmacy::setFormVlas(string strFormVlas) { try { char* chFormVlas{}; int iCountFormVlas = 0; if ((chFormVlas = (char*)calloc(60, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chFormVlas[i] = strFormVlas[i]; if (checkNumbers(strFormVlas)) throw chFormVlas; if (strFormVlas[i] == '\0') break; iCountFormVlas++; if (iCountFormVlas > 60) throw iCountFormVlas; } if (!islower(strFormVlas[0])) { this->strFormVlas = strFormVlas; } else throw strFormVlas; } catch (char* chFormVlas) { cout << "Error invalid WorkingKlas!!! Wright it without numbers or other chars" << endl; } catch (string strFormVlas) { cout << "Error invalid Town!!! Wright it from big letter" << endl; } catch (int iCountFormVlas) { cout << "Error invalid Town!!! Too much chars" << endl; } } 

Parent Class Code

 #include <iostream> #include <fstream> #include <string> using namespace std; class BasikZaklad { private: string strName, strTown, strStreet, strNumberBuilding; public: BasikZaklad() {}; BasikZaklad(const BasikZaklad &obj) { setName(strName); setTown(strTown); setStreet(strStreet); setNumberBuilding(strNumberBuilding); } ~BasikZaklad() {}; virtual void Show() { cout << "string strName ==" << getName() << endl; cout << "string strTown ==" << getTown() << endl; cout << "string strStreet ==" << getStreet() << endl; cout << "string strNumberBuilding ==" << getNumberBuilding() << endl; } bool checkNumbers(string strSome); //функция для проверки на номера bool checkNumbersAdress(string strSome1); //функция для проверки Adress //зміна полів класу void setName(string strName); void setTown(string strTown); void setStreet(string strStreet); void setNumberBuilding(string strNumberBuilding); //Зчитування полів string getName(void) { return strName; } string getTown(void) { return strTown; } string getStreet(void) { return strStreet; } string getNumberBuilding(void) { return strNumberBuilding; } }; bool BasikZaklad::checkNumbers(string strSome) { char chNumbers[32] = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', '/', ';','<', '>', ',', '.', '?', ']', '[', '{', '}', '!', '@', '#', '%', '^', '&', '(', ')', '_', '+', '=' }; for (int i = 0; ; i++) { for (int j = 0; j < 32; j++) { if (strSome[i] == chNumbers[j]) { return true; } } if (strSome[i] == '\0') break; } return false; } bool BasikZaklad::checkNumbersAdress(string strSome1) { char chNumbers[21] = { ';','<', '>', ',', '.', '?', ']', '[', '{', '}', '!', '@', '#', '%', '^', '&', '(', ')', '_', '+', '=' }; for (int i = 0; ; i++) { for (int j = 0; j < 21; j++) { if (strSome1[i] == chNumbers[j]) { return true; } } if (strSome1[i] == '\0') break; } return false; } void BasikZaklad::setName(string strName) { try { char* chName{}; int iCountName = 0; if ((chName = (char*)calloc(40, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chName[i] = strName[i]; if (checkNumbersAdress(strName)) throw chName; if (strName[i] == '\0') break; iCountName++; if (iCountName > 40) throw iCountName; } if (!islower(strName[0])) { this->strName = strName; } else throw strName; } catch (char* chName) { cout << "Error invalid Name!!! Wright it without numbers or other chars" << endl; } catch (string strName) { cout << "Error invalid Name!!! Wright it from big letter" << endl; } catch (int iCountName) { cout << "Error invalid Name!!! Too much chars" << endl; } } void BasikZaklad::setTown(string strTown) { try { char* chTown{}; int iCountTown = 0; if ((chTown = (char*)calloc(40, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chTown[i] = strTown[i]; if (checkNumbers(strTown)) throw chTown; if (strTown[i] == '\0') break; iCountTown++; if (iCountTown > 40) throw iCountTown; } if (!islower(strTown[0])) { this->strTown = strTown; } else throw strTown; } catch (char* chTown) { cout << "Error invalid Town!!! Wright it without numbers or other chars" << endl; } catch (string strTown) { cout << "Error invalid Town!!! Wright it from big letter" << endl; } catch (int iCountTown) { cout << "Error invalid Town!!! Too much chars" << endl; } } void BasikZaklad::setStreet(string strStreet) { try { char* chStreet{}; int iCountStreet = 0; if ((chStreet = (char*)calloc(40, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chStreet[i] = strStreet[i]; if (checkNumbers(strStreet)) throw chStreet; if (strStreet[i] == '\0') break; iCountStreet++; if (iCountStreet > 40) throw iCountStreet; } if (!islower(strStreet[0])) { this->strStreet = strStreet; } else throw strStreet; } catch (char* chStreet) { cout << "Error invalid Street!!! Wright it without numbers or other chars" << endl; } catch (string strStreet) { cout << "Error invalid Street!!! Wright it from big letter" << endl; } catch (int iCountStreet) { cout << "Error invalid Street!!! Too much chars" << endl; } } void BasikZaklad::setNumberBuilding(string strNumberBuilding) { try { char* chNumberBuilding{}; int iCountNumberBuilding = 0; if ((chNumberBuilding = (char*)calloc(10, sizeof(char))) == 0) { printf("\nMemory allocation failure\n"); exit(1); } for (int i = 0; ; i++) { chNumberBuilding[i] = strNumberBuilding[i]; if (checkNumbersAdress(strNumberBuilding)) throw chNumberBuilding; if (strNumberBuilding[i] == '\0') break; iCountNumberBuilding++; if (iCountNumberBuilding > 10) throw iCountNumberBuilding; } this->strNumberBuilding = strNumberBuilding; } catch (char* chNumberBuilding) { cout << "Error invalid NumberBuilding!!! Wright it without other chars" << endl; } catch (int iCountNumberBuilding) { cout << "Error invalid NumberBuilding!!! Too much chars" << endl; } } 

I watched in Vizhalka for filling, all objects in the stack are empty.

1 answer 1

In the copy constructors, the fields of the copied object are not used; instead, they are used with empty fields of this object. Base class copying does not occur at all. It should be:

 Pharmacy(const Pharmacy &obj) : BasikZaklad{obj} , strWorkingKlas{obj.strWorkingKlas} , strFormVlas{obj.strFormVlas} {} 

In fact, it is not advisable to implement the manual copy constructor in this case, you can give it to the copiler:

 Pharmacy(const Pharmacy &obj) = default; 

You should always read (and eliminate the causes) compiler warnings. In this case, he probably complained about the unused argument obj .

  • No, he did not complain about the arguments, except those in the catch blocks, only they are unused. - Vereh
  • @Vereh You may have warnings disabled. - VTT
  • All warnings are included, I try to follow them - Vereh
  • @Vereh And what about the compiler? An unused function argument, with all warnings enabled, should trigger a warning in g ++, clang ++, and vc ++. - VTT
  • I have Microsoft Visual Studio 17 years old - Vereh 1:51 pm