1

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?

2
  • If you get errors about non-POD element types, you're using C++, not C. Commented Apr 1, 2015 at 4:35
  • I think in C++ you can't have a variable_name as a array size for a VLA. It has to be a constant int max = 100; int a[max]; is not allowed whereas #define max 100 int a[max]; should be fine Commented Apr 1, 2015 at 4:38

7 Answers 7

2

The C++ standard does not support variable length arrays. If you need variable length array functionality, use vector<Books> instead.

G++ allows VLAs as an extension to standard C++. However, you cannot initialize VLAs in C, nor in G++'s dialect of C++. So the elements of a VLA cannot have (non-trivial) constructors. And the error message is telling you that:

variable length array of non-POD element type 'Books' Books booklist[MAX_SIZE];

You have a VLA because MAX_SIZE is not const int MAX_SIZE = 100. You can't create a VLA of type Books because the string members have constructors (are not POD — Plain Old Data — types), and hence there's a non-trivial constructor for the type Books.

The simplest fix is to use:

    const int MAX_SIZE = 100;
    int size;
    int choice;

Or use:

std::vector<Books> booklist;
Sign up to request clarification or add additional context in comments.

Comments

1

Declare MAX_SIZE as a const int and it should work. The issue is that the size of the array has to be known at compile time (it has to be a compile time constant). An int can be changed during runtime while a const int (or a define) cannot be.

Comments

1

While declaring the structure you have to give like this.

struct Books booklist[MAX_SIZE];

Or else make the typedef in headerfile.

typedef struct Books
{
    int ISBN;
    string Author;
    string Publisher;
    int Quantity;
    double Price;
}Books;

Make the value of MAX_SIZE like this.

#define MAX_SIZE 100

1 Comment

While MAX_SIZE is not a constant integer, this won't help.
0

Just conver the following line to define

int MAX_SIZE = 100

So the solution would be

#define MAX_SIZE 100

1 Comment

Ugh! const int MAX_SIZE = 100; would be way better; enum { MAX_SIZE = 100 }; would be better. Avoid #define in C++.
0

If you aren't using typedef, u need to specify the type as struct <struct_name>

int main()
{
    int MAX_SIZE = 100, size, choice;
    struct Books novels;
    struct Books booklist[MAX_SIZE];
}

Comments

0

Some pointers below
a. I think it is a typo, the header file must contain #endif statement.
b. To create an array on stack, the array size must be a const , try changing MAX_SIZE to const int MAX_SIZE = 100.

Comments

0

Regarding VLA:

  • If your code is C++ (This question)

AFAIK, there is nothing in C++ standard as VLA support. Maybe std::vector will help.

Solution: For this code, you can change the int MAX_SIZE = 100 to a #define statement, like #define MAX_SIZE 100 or, make MAX_SIZE of type const int.


  • If your code is C (As tagged earlier)

Point to note: As per your code, Books is not a data type, struct Books is.

So, use either of below:

  • use struct Books in your code
  • use typedef struct Books Books; and then use Books as you've used in your code.

Also, as far as C standard is concerned, VLA is introduced in C99 standard. You have to enforece the standard by supplying --std=c99 with the gcc.

3 Comments

The code is C++, not C — witness the headers, the using namespace std; etc. (Yes, the question was once tagged with C too, but that was a mistake.)
I would put sub-headings like ### If your code is C before the C material, and ### If your code is C++ before the C++ material, but the code was already C++, so the C information, although accurate, isn't really relevant. The C++ suggestions are OK.
Note that in C++, Books is a type once struct Books { has been parsed. Your opening paragraphs are part of your C spiel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.