Struct type "does not provide a subscript operator"

jshapy8 picture jshapy8 · Apr 1, 2015 · Viewed 29.5k times · Source

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.

The struct is contained in a header file while declaration of the array of structs is in main(). Here is the (relevant) code from the functions.h header file:

#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:

#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

Answer

user3335 picture user3335 · Apr 1, 2015

There are too many problems in your code, you define the READ_INVENTORY as a global function. So you might have received that there is an undefined reference to functions::READ_INVENTORY. Another problem is you pass Books instead of Books* so you can't use the [] operator.

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);