在另一个成员函数中调用成员函数时'int'之前的预期主表达式

Expected primary-expression before 'int' when calling a member function inside another member function

本文关键字:函数 成员 表达式 int 调用 另一个      更新时间:2023-10-16

我刚刚开始学习 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;
    }

我已经清除了您询问的基本错误。但是你应该自己纠正逻辑上的错误。

希望你会。