在c++中期望主表达式在token之前
expecting primary expression before token in c++
问题是:
在console.cppvoid 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;
}
表达式Medicine.getID()
不正确。Medicine
是类的名称,不能使用点运算符访问其成员。首先,你需要一个Medicine
的实例,你想访问它的成员;其次,如果您有一个指向该实例的指针,则需要使用箭头操作符(operator ->
)。
因此,它应该是:
void Console::PrintMedicine(Medicine* m){
int ID = m->getID();
// ^^^
// ...
}
相关文章:
- 如何确保C++函数在定义之前声明(如override关键字)
- 为什么在定义函数之前先声明它
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 程序在执行程序的其余部分之前退出
- 如何将模板转换为C++11之前的模板
- '||'之前的预期主要表达
- (C++)为什么静态成员可以在初始化之前使用
- 标记 '","' 之前的预期主表达式
- C++的解析器在可以区分比较和模板实例化之前会做什么?
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- 错误:在"生成字符串"之前应为";"
- 预计“ [''token之前,预计无限制的ID
- 奇怪的错误:标签'loopend'使用但未定义," if "之前应为非限定 id("goto"和" '}' token "错误相同
- 'token'之前的预期初始值设定项
- 期望')'之前';' token g++
- 编译错误:solution.c:20:5: Error:期望的标识符或'('在' {' token{之前
- 在c++中期望主表达式在token之前
- Arduino错误:* token之前预期的初始化器
- {token c++之前的期望类名
- 在{token之前期望的类名-非常简单