期望在c ++中令牌之前的主表达式

时间:2013-03-31 21:57:39

标签: c++

问题在于:

在console.cpp中

void Console::PrintMedicine(Medicine* m){
    int ID = Medicine.getID(); //here i get the error expecting primary expression before "." token, it doesn't work with -> either
    cout<<"The Medicine's ID is: ";
    cin>>ID;
}
类医学:

什么是私人的:

private:
    int ID;
    std::string nume;
    double concentratie;
    int cantitate;

什么是公开

public:
    Medicine();  //implcit constructor
    Medicine(int ID, std::string nume, double concentratie, int cantitate);//constructor with parameteres
    ~Medicine(); //destructor

// getID函数

        const int& Medicine::getID() const{
    return this->ID;
}

// getName函数

const std::string& Medicine::getName() const{
    return this->nume;
}

// getConcentration函数

const double& Medicine::getConcentration() const{ 
    return this->concentratie;

}

// getQuantity函数

const int& Medicine::getQuantity() const{
    return this->cantitate;
}

1 个答案:

答案 0 :(得分:2)

表达式Medicine.getID()不正确。 Medicine的名称,您不能使用点运算符来访问其成员。首先,您需要Medicine实例,您要访问其成员;第二,如果你有一个指针到那个实例,你需要使用箭头操作符(operator ->)。

因此,它应该是:

void Console::PrintMedicine(Medicine* m){
    int ID = m->getID();
//           ^^^

// ...
}
相关问题