在另一个成员函数中调用成员函数时,在“int”之前的预期主表达式
Posted
技术标签:
【中文标题】在另一个成员函数中调用成员函数时,在“int”之前的预期主表达式【英文标题】:Expected primary-expression before 'int' when calling a member function inside another member function 【发布时间】:2019-07-24 13:58:33 【问题描述】:我刚开始学习 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;
【问题讨论】:
投票结束是一个错字。在main
的调用和函数中删除int money
。只需在函数体中添加int money;
。
@NathanOliver,这个错误出现在两个地方,这表明有一个关于如何使用参数调用函数的教训。如果要关闭它,我敢肯定那里有一个副本,至少可以将 Advait 转发给答案。
@Romen SO 不能替代一本好的参考书。我们确实希望一些已经知道的基本知识以及如何调用函数是,至少恕我直言,一个非常基本的概念。
@NathanOliver 我尝试了您的解决方案并编译了程序,但它创建了一个无限循环。还有什么我可以尝试的吗?
Error "expected primary-expression before int"的可能重复
【参考方案1】:
好吧,代码中有很多错误。我之前没有完全看到他们的程序。 我认为你可能做对了。那好吧。
首先,您应该在程序开始时询问机器可以接受哪些硬币,而您没有。
其次,在按值调用函数之前,您应该从用户那里获取输入,然后将其传递给函数,而您也没有这样做。
在这里,我已经稍微更正了您的代码。
#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;
我已经清除了您询问的基本错误。但逻辑上的错误你应该自己改正。
希望你会。
【讨论】:
感谢您更正我的代码!我会尝试解决逻辑错误 欢迎,if(stuck.==true)then ask;以上是关于在另一个成员函数中调用成员函数时,在“int”之前的预期主表达式的主要内容,如果未能解决你的问题,请参考以下文章