jshapy8
jshapy8

Reputation: 2051

How to create an array of structs in main() when struct definition is in a header file?

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?

Upvotes: 1

Views: 3011

Answers (7)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286


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.

Upvotes: 0

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

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

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753645

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;

Upvotes: 2

kumar_m_kiran
kumar_m_kiran

Reputation: 4022

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.

Upvotes: 0

thegeeklife
thegeeklife

Reputation: 107

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];
}

Upvotes: 0

Alan Wolfe
Alan Wolfe

Reputation: 645

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.

Upvotes: 1

Yasir Majeed
Yasir Majeed

Reputation: 741

Just conver the following line to define

int MAX_SIZE = 100

So the solution would be

#define MAX_SIZE 100

Upvotes: 0

Related Questions