简体   繁体   English

结构类型“不提供下标运算符”

[英]Struct type “does not provide a subscript operator”

I am trying to read values from a file into an array of structs. 我试图从文件中读取值到结构数组。 However, I keep getting compiler errors that tell me that my struct, Books, does not provide a subscript operator and I am lost. 但是,我不断收到编译器错误,告诉我我的struct,Books,没有提供下标操作符,我迷路了。

The struct is contained in a header file while declaration of the array of structs is in main(). 结构包含在头文件中,而结构数组的声明在main()中。 Here is the (relevant) code from the functions.h header file: 这是functions.h头文件中的(相关)代码:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Books
{
        int ISBN;
        string Author;
        string Publisher;
        int Quantity;
        double Price;
};


class functions
{
        public:
                void READ_INVENTORY(Books, int, int);

};


// Function definitions

void READ_INVENTORY(Books array, int max, int position)
{
        ifstream inputFile;
        inputFile.open("inventory.dat");

        inputFile >> array[position].ISBN;
        inputFile >> array[position].Author;
        inputFile >> array[position].Publisher;
        inputFile >> array[position].Quantity;
        inputFile >> array[position].Price;

        cout << "The following data was read from inventory.dat:\n\n"
             << "ISBN: " << array[position].ISBN << endl
             << "Author: " << array[position].Author << endl
             << "Publisher: " << array[position].Publisher << endl
             << "Quantity: " << array[position].Quantity << endl
             << "Price: " << array[position].Price << endl << endl;
}

And here is the array of struct declaration in main along with how it is used: 这里是main中的struct声明数组及其使用方式:

#include <iostream>
#include <string>
#include <fstream>
#include "functions.h"
using namespace std;

int main()
{
        const int MAX_SIZE = 100;
        int size, choice;
        functions bookstore;
        Books booklist[MAX_SIZE];

        cout << "Select a choice\n\n";

            cin >> choice;

            size = choice;

            switch (choice)
            {
                    case 1: bookstore.READ_INVENTORY(booklist[choice], MAX_SIZE, size);
                            break;

             }
}

Once compiled, I get 10 error messages (one for each time I use array[position]) that state: error: type 'Books' does not provide a subscript operator 编译完成后,我收到10条错误消息(每次使用一个数组[position]时)状态: 错误:类型'Books'不提供下标运算符

There are too many problems in your code, you define the READ_INVENTORY as a global function. 您的代码中存在太多问题,您将READ_INVENTORY定义为全局函数。 So you might have received that there is an undefined reference to functions::READ_INVENTORY . 所以你可能已经收到了functions::READ_INVENTORY的未定义引用。 Another problem is you pass Books instead of Books* so you can't use the [] operator. 另一个问题是您传递Books而不是Books*因此您无法使用[]运算符。

Change this 改变这个

void READ_INVENTORY(Books array, int max, int position) 
{

to

void functions::READ_INVENTORY(Books* array, int max, int position)
{

Now that we have changed the parameter type, change this line 现在我们已经更改了参数类型,请更改此行

case 1: bookstore.READ_INVENTORY(booklist[choice], MAX_SIZE, size);

to

case 1: bookstore.READ_INVENTORY(booklist, MAX_SIZE, size);

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

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