Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 271
jshapy8 Avatar asked Apr 01 '15 06:04

jshapy8


People also ask

What is a subscript operator?

The subscript operator is defined as square brackets [] . It is used to access the elements of string, list tuple, and so on. Let's discuss the syntax with an example: <given string>[<index>] The given string is the string you want to examine and the index is the position of the character you want to obtain.

Why does the function overload the subscript operator return by reference?

Because the m_list member variable is private, we can not access it directly from variable list. This means we have no way to directly get or set values in the m_list array.


1 Answers

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);
like image 175
user3335 Avatar answered Sep 29 '22 00:09

user3335