在另一个成员函数中调用成员函数时'int'之前的预期主表达式
Expected primary-expression before 'int' when calling a member function inside another member function
我刚刚开始学习 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++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针