简体   繁体   English

如何在C ++中修复“错误:')'标记之前的预期主表达式”?

[英]How to fix “error: expected primary -expression before ')' token” in c++?

I am writing this code, and I have 2 structures and 1 function. 我正在编写此代码,我有2个结构和1个函数。 The function takes in one of the structures as reference and uses the variables in it. 该函数采用一种结构作为参考,并在其中使用变量。 The problem is that I keep getting the following 2 errors: 问题是我不断收到以下2个错误:

error: expected primary-expression before ')' token
error: 'arrayDrink' was not declared in this scope

I don't know what I am doing wrong. 我不知道我在做什么错。

I tried the other structure too, but I keep getting this error: 我也尝试了其他结构,但仍然出现此错误:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;
const unsigned int maxDrinkItems = 20;

struct DrinkItem
{
    unsigned int id;
    string name;
    double price;
    unsigned int NumDrinksOfSameType;
    unsigned int drinksPurchased = 0;

};

struct DrinkMachine
{
    unsigned int versionNumber = 1;
    unsigned int totalDrinks;
    unsigned int arrayDrink[maxDrinkItems];
};

bool create(DrinkMachine &drinkMachine)
{
    ifstream inputFile;
    inputFile.open("drink_machine.txt");

    if(inputFile.fail())
    {
        return false;
    }
    else
    {

        while (inputFile.eof() || drinkMachine.totalDrinks == 20)
        {
            inputFile >> drinkMachine.totalDrinks;

            for (int i = 0; i < drinkMachine.totalDrinks; i++)
            {
                inputFile >> arrayDrink[i].name;
                inputFile >> arrayDrink[i].price;
                inputFile >> arrayDrink[i].NumDrinksOfSameType;
            }

        }

        inputFile.close();
        return true;
    }

}

here I call the function: 在这里我调用该函数:

int main()
{
    create(DrinkMachine);

    return 0;
}

I want it to take in data from the file and put it into the array of structures, but I keep getting errors. 我希望它从文件中获取数据并将其放入结构数组中,但是我一直在出错。

The condition in the while loop doesn't work the way you think. while循环中的条件不符合您的想法。

while (inputFile.eof() || drinkMachine.totalDrinks == 20)

When doing input extraction in a loop you should first do the extraction, check if it succeeds, then continue. 在循环中进行输入提取时,应首先进行提取,检查是否成功,然后继续。 eof() checks if the eofbit (end-of-stream bit) in the stream is set, which occurs when the previous extraction failed. eof()检查流中的eofbit(流的末尾位)是否已设置,这在上一次提取失败时发生。 Typically when doing extraction you check stream validity with fail() . 通常,在进行提取时,您会使用fail()检查流有效性。

while (inputFile >> drinkMachine.totalDrinks && !inputFile.fail() && drinkMachine.totalDrinks == 20)

We can get rid of !inputFile.fail() since every stream has a built in operator bool() so it checks fail() implicitly: 我们可以摆脱!inputFile.fail()因为每个流都有内置的operator bool()所以它隐式地检查fail()

while (inputFile >> drinkMachine.totalDrinks && drinkMachine.totalDrinks == 20)

Your other issues are pointed out by Acorn. 您的其他问题由Acorn指出。

There are several errors in your code. 您的代码中有几个错误。

First, you need to access your drinkMachine object to get to arrayDrink here: 首先,您需要访问您的drinkMachine对象才能在此处进入arrayDrink

{
    inputFile >> arrayDrink[i].name;
    inputFile >> arrayDrink[i].price;
    inputFile >> arrayDrink[i].NumDrinksOfSameType;
}

See what you do in the loop: 查看循环中的操作:

for (int i = 0; i < drinkMachine.totalDrinks; i++)

Another one: you are trying to call create with a type, rather than an object: 另一个:您尝试使用类型而不是对象来调用create

create(DrinkMachine);

Instead, you need to define a DrinkMachine object and then pass it to create . 相反,您需要定义DrinkMachine对象,然后将其传递给create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM