c++ - How to create an array of structs in main() when struct definition is in a header file? -
i creating program takes bookstore inventory , each individual item isbn , author in struct called books. since there multiple books within inventory, want create array of books struct. because of outside requirement beyond control, struct definition must in header file class resides , array of structs must declared within main().
here struct definition in header file functions.h:
#ifndef functions_h #define functions_h #include <iostream> #include <string> using namespace std; struct books { int isbn; string author; string publisher; int quantity; double price; };
now try create array of structs in main(). note allows me create variable struct books, not array:
#include <iostream> #include <string> #include <fstream> #include "functions.h" using namespace std; int main() { int max_size = 100, size, choice; functions bookstore; books novels; books booklist[max_size]; }
when this, following compiler error
bookstore.cpp:11:16: error: variable length array of non-pod element type 'books' books booklist[max_size];
why getting such error when trying declare array of structs outside struct, not variable same outside struct?
declare max_size const int , should work. issue size of array has known @ compile time (it has compile time constant). int can changed during runtime while const int (or define) cannot be.
Comments
Post a Comment