7

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

jshapy8
  • 1,983
  • 7
  • 30
  • 60
  • 1
    `Books array` is not an array. You should pass `Books* array`. Currently you just pass one Book and that has obviously no []. BTW, you should improve the variable name, array is not very descriptive; how about `books`? – usr1234567 Apr 01 '15 at 06:45
  • `array` isn't an array... – user253751 Apr 01 '15 at 06:53

1 Answers1

5

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);
Fish
  • 1,205
  • 1
  • 10
  • 12