首页 > 解决方案 > 将结构传递给函数时遇到问题

问题描述

我正在尝试将结构传递给函数并遇到错误“预期的主表达式之前'。' 令牌”。我无法弄清楚我将如何传递结构?任何帮助深表感谢!

void check(std::string buildingName, int floorLevel, std::string drinkName, float drinkSize, struct machines)
{
        bool correct = true;
        if (buildingName == "Snell")
        {
                if (floorLevel == 1 || floorLevel == 3)
                {
                        if (floorLevel == 1)
                        {
                                for (int i = 0; i < 10; i++)
                                {
                                        if (machines.vendingMachines[0].drinkTypes[i].drinkName == drinkName && machines.vendingMachines[0].drinkTypes[i].drinkSize == drinkSize)
                                        {
                                                correct = true;
                                        }
                                }
                        }
                        else
                        {
                                for (int i = 0; i < 10; i++)
                                {
                                        if (machines.vendingMachines[2].drinkTypes[i].drinkName == drinkName && machines.vendingMachines[2].drinkTypes[i].drinkSize == drinkSize)
                                        {
                                                correct = true;
                                        }
                                }
                        }
                }
                else
                {
                        correct = false;
                }
        }
        else
        {
                correct = false;
        }
}

标签: c++

解决方案


在您的代码的某些行中,您使用 vendingMachines 作为 Machines 的对象

machines.vendingMachines[2].drinkTypes[i].drinkSize

在其他一些行中,您没有使用 vendingMachines 作为 Machines 的对象

vendingMachines[2].drinkTypes[i].drinkName

尽管如此,我没有看到任何声明 VendingMachines 是结构机器的对象。


推荐阅读