c++ - Struct type "does not provide a subscript operator" -
i trying read values file array of structs. however, keep getting compiler errors tell me struct, books, not provide subscript operator , lost.
the struct contained in header file while declaration of array of structs in main(). here (relevant) code 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 read 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 array of struct declaration in main along how 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 choice\n\n"; cin >> choice; size = choice; switch (choice) { case 1: bookstore.read_inventory(booklist[choice], max_size, size); break; } }
once compiled, 10 error messages (one each time use array[position]) state: error: type 'books' not provide subscript operator
there many problems in code, define read_inventory
global function. might have received there undefined reference functions::read_inventory
. problem pass books
instead of books*
can't use []
operator.
change
void read_inventory(books array, int max, int position) {
to
void functions::read_inventory(books* array, int max, int position) {
now have changed parameter type, change line
case 1: bookstore.read_inventory(booklist[choice], max_size, size);
to
case 1: bookstore.read_inventory(booklist, max_size, size);
Comments
Post a Comment