I am creating a program that takes bookstore inventory and each individual item like the ISBN and author is in a struct called Books. Since there will be multiple books within this inventory, I want to create an array of the Books struct. Because of an outside requirement beyond my control, the struct definition must be in the header file where my class resides and the array of structs must be declared within main().
Here is the struct definition in the 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 I try to create the array of structs back in main(). Note that it allows me to create a variable from the struct Books, but not an 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 I do this, I get the following compiler error
bookstore.cpp:11:16: error: variable length array of non-POD element type 'Books' Books booklist[MAX_SIZE];
Why am I getting such an error when trying to declare an array of structs from an outside struct, but not a variable from the same outside struct?
int max = 100; int a[max];is not allowed whereas#define max 100 int a[max];should be fine