我有一个包含药物的列表(动态矢量),我想从列表中打印所有药物,但它会从内存中打印一些数字。例如:
Give the medicine's ID: 1
Give the medicine's name:alg
Give the medicine's concentration: 30
Give the medicine's quantity: 40
打印:
The medicine's ID is: 4215400
The medicine's name is:
The medicine's concentration is: 3.47235e+230
The medicine's quantity is: 2686544
这些是打印功能:
void Console::printMedicine(Medicine* m){
int ID = m->getID();
string name = m->getName();
double concentration = m->getConcentration();
int quantity = m->getQuantity();
cout<<"\nThe medicine's ID is: "<<ID<<"\n";
cout<<"The medicine's name is: "<<name<<"\n";
cout<<"The medicine's concentration is: "<<concentration<<"\n";
cout<<"The medicine's quantity is: "<<quantity<<"\n";
}
void Console::printAllMedicines(){
DynamicVector<Medicine*>* medList = ctrl->getAllMeds();
for(int i=0; i < medList->getLen(); i++){
Medicine* m = medList->getElementAtPosition(i);
printMedicine(m);
}
}
这是getElementAtPosition函数:
template <typename Element>
Element DynamicVector<Element>::getElementAtPosition(int pos){
return this->elems[pos];
}
这是医学课,只有私人部分:
class Medicine{
private:
int ID, quantity;
double concentration;
string name;
这是存储库中medList的声明:
class Repository{
public:
DynamicVector<Medicine*>* medList;
医学课中的构造函数和getter:
Medicine::Medicine(){ //implicit constructor
this->ID=0;
this->concentration=0;
this->name="";
this->quantity=0;
}
Medicine::Medicine(int ID, string name, double concentration, int quantity){ //constructor with parameters
this->ID=ID;
this->name=name;
this->concentration=concentration;
this->quantity=quantity;
int Medicine::getID(){
return this->ID;
}
string Medicine::getName(){
return this->name;
}
double Medicine::getConcentration(){
return this->concentration;
}
int Medicine::getQuantity(){
return this->quantity;
}
getAllMeds和getAll函数:
DynamicVector<Medicine*>* Controller::getAllMeds(){
return repo->getAll();
}
DynamicVector<Medicine*>* Repository::getAll(){
return medList;
}
我试了几个小时来解决这个问题,但我无法理解问题出在哪里。我做错了什么?
答案 0 :(得分:0)
我假设您的Medicine实例是由getAllMeds()创建的。您确定要将好的值传递给构造函数吗?你看到的那些价值观对我来说就像是未初始化的记忆。它们每次都是相同的奇怪值,还是会改变?