-1

我有一个包含药物的列表(动态向量),我想打印列表中的所有药物,但它会从内存中打印一些数字。例如:

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;

医学类中的构造函数和吸气剂:

  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;
}

我尝试了几个小时来解决这个问题,但我就是不明白问题出在哪里。我究竟做错了什么?

4

1 回答 1

0

我假设您的 Medicine 实例是由 getAllMeds() 创建的。你确定你将好的值传递给你的构造函数吗?您看到的那些值对我来说看起来像是未初始化的内存。它们每次都是相同的奇怪值还是会改变?

于 2013-04-05T12:36:34.490 回答