c++ - 在另一个成员函数中调用成员函数时,在“int”之前的预期主表达式
问题描述
我刚刚开始学习 c++,并认为我会尝试通过做一个小项目来衡量我的理解,我正在使用类和成员函数创建一个模拟自动售货机的程序。我只收到以下 2 个错误:“int”之前的预期主要期望
我尝试删除 int,但它给了我对变量错误的未定义引用。任何帮助是极大的赞赏!
#include <iostream>
class DrinkMachine
{
private:
const int COST_OF_DRINK = 150;
public:
int RunningTotal;
DrinkMachine()
{
RunningTotal = 0;
}
void DepositCoins(int money)
{
std::cout << "You can deposit coins of values 25, 50 and 100" << std::endl;
std::cout << "Enter a coin: " << std::endl;
switch(money)
{
case(25):
RunningTotal += 25;
break;
case(50):
RunningTotal += 50;
break;
case(100):
RunningTotal += 100;
break;
default:
std::cout << "You entered the wrong coin" << std::endl;
std::cout << "Coins of values 25, 50, 100 are allowed" << std::endl;
break;
}
}
bool CheckTotal()
{
if(RunningTotal >= COST_OF_DRINK)
return true;
else
return false;
}
void MakeDrinkSelection(int DrinkChoice)
{
bool IsChoiceValid = false;
while(!IsChoiceValid)
{
switch(DrinkChoice)
{
case (1):
std::cout << "Thank you for choosing Coffee!" << std::endl;
ReturnChange();
IsChoiceValid = true;
break;
case(2):
std::cout << "Thank you for choosing Hot Chocolate!" << std::endl;
IsChoiceValid = true;
ReturnChange();
break;
case(3):
std::cout << "Thank you for choosing Green Tea!" << std::endl;
IsChoiceValid = true;
ReturnChange();
break;
default:
std::cout << "Invalid selection. Please re-enter your choice" << std::endl;
bool NewDrinkChoice;
DrinkChoice = NewDrinkChoice;
IsChoiceValid = false;
break;
}
}
}
void DisplayDrinks()
{
std::cout << "----------------" << std::endl;
std::cout << "1. Coffee" << std::endl;
std::cout << "2. Hot Chocolate" << std::endl;
std::cout << "3. Green Tea" << std::endl;
std::cout << "----------------" << std::endl;
std::cout << std::endl;
std::cout << "Please make a choice: " << std::endl;
MakeDrinkSelection(int DrinkChoice); //gives an error
}
void ReturnChange()
{
if(RunningTotal > COST_OF_DRINK)
{
std::cout << "Your change is: " << (RunningTotal - COST_OF_DRINK) << std::endl;
}
}
};
int main()
{
//DrinkMachine drinkmachine = new DrinkMachine();
DrinkMachine mydrink;
while(!mydrink.CheckTotal())
{
mydrink.DepositCoins(int money); //gives an error
}
mydrink.DisplayDrinks();
return 0;
}
解决方案
好吧,代码中有很多错误。我之前没有完全看到他们的程序。我认为你可能做对了。那好吧。
首先,您应该在程序开始时询问机器可以接受哪些硬币,而您没有。
其次,在按值调用函数之前,您应该从用户那里获取输入,然后将其传递给函数,而您又没有这样做。
在这里,我稍微更正了您的代码。
#include <iostream>
class DrinkMachine
{
private:
const int COST_OF_DRINK = 150;
public:
int RunningTotal;
DrinkMachine()
{
RunningTotal = 0;
}
void DepositCoins(int money)
{
switch(money)
{
case(25):
RunningTotal += 25;
break;
case(50):
RunningTotal += 50;
break;
case(100):
RunningTotal += 100;
break;
default:
std::cout << "You entered the wrong coin" << std::endl;
std::cout << "Coins of values 25, 50, 100 are allowed" << std::endl;
break;
}
}
bool CheckTotal()
{
std::cout << "You can deposit coins of values 25, 50 and 100" << std::endl;
std::cout << "Enter a coin: " << std::endl;
if(RunningTotal >= COST_OF_DRINK)
return true;
else
return false;
}
void MakeDrinkSelection(int DrinkChoice)
{
bool IsChoiceValid = false;
while(!IsChoiceValid)
{
switch(DrinkChoice)
{
case (1):
std::cout << "Thank you for choosing Coffee!" << std::endl;
ReturnChange();
IsChoiceValid = true;
break;
case(2):
std::cout << "Thank you for choosing Hot Chocolate!" << std::endl;
IsChoiceValid = true;
ReturnChange();
break;
case(3):
std::cout << "Thank you for choosing Green Tea!" << std::endl;
IsChoiceValid = true;
ReturnChange();
break;
default:
std::cout << "Invalid selection. Please re-enter your choice" << std::endl;
bool NewDrinkChoice;
DrinkChoice = NewDrinkChoice;
IsChoiceValid = false;
break;
}
}
}
void DisplayDrinks()
{
int DrinkChoice; //here declared
std::cout << "----------------" << std::endl;
std::cout << "1. Coffee" << std::endl;
std::cout << "2. Hot Chocolate" << std::endl;
std::cout << "3. Green Tea" << std::endl;
std::cout << "----------------" << std::endl;
std::cout << std::endl;
std::cout << "Please make a choice: " << std::endl;
std::cin>>DrinkChoice; //input by user
MakeDrinkSelection(DrinkChoice); //used to give an error
}
void ReturnChange()
{
if(RunningTotal > COST_OF_DRINK)
{
std::cout << "Your change is: " << (RunningTotal - COST_OF_DRINK) << std::endl;
}
}
};
int main()
{
//DrinkMachine drinkmachine = new DrinkMachine();
DrinkMachine mydrink;
int money; //here declared
while(!mydrink.CheckTotal())
{
std::cin>>money; //input by user
mydrink.DepositCoins(money); //no error
}
mydrink.DisplayDrinks();
return 0;
}
我已经清除了您询问的基本错误。但是你应该自己纠正逻辑上的错误。
希望你会。
推荐阅读
- c# - Unity中如何访问TextMeshProUGUI
- javascript - Next.js : 服务器端渲染页面中请求高时无响应(动态路由)
- kotlin - Android Kotlin 双向数据绑定 BR 类错误
- wordpress - 如何在元区域的帖子正上方显示 wordpress 帖子的类别?
- python - Pandas 按过滤条件采样
- python - 如何在 Google Colab 中更新 scikit-learn?
- python - Matplotlib - 在三角形区域上绘图
- mongodb - MongoDB中的管道功能,如何进行连接
- javascript - vscode 格式不格式化
- r - R中的矩阵/数据帧循环问题