我有一個包含藥物的列表(動態矢量),我想從列表中打印所有藥物,但它從內存中打印一些數字。例如:從內存中打印奇怪的數字
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;
}
我嘗試了幾個小時來解決這個問題,但我不明白問題在哪裏。我究竟做錯了什麼?
這是一個非常方便的調試器。 – NPE 2013-04-05 12:19:51
我們可以看到'Medicine'類的構造函數和'get *()'函數嗎? – Shoe 2013-04-05 12:20:16
除非它是爲了學習功課,爲什麼當[[std :: vector]](http://en.cppreference.com/w/cpp/container/vector)創建自己的向量? – 2013-04-05 12:22:39