dann_
dann_

Reputation: 47

using global variable giving error

Here, i am trying to create an struct with key[2*d] and ptr[2*d+1] but the compiler is giving error with these lines of code only saying:

"error:data members may not have variably modified type."

I want to use 'd' only in my code. Now how to fix it.

#include<iostream>
using namespace std;
static int d=1;

struct Btree{
    public:
    int key[2*d];
    int count;
    Btree *ptr[2*d+1];
    Btree *pptr;
};

Upvotes: 0

Views: 100

Answers (1)

Alec Teal
Alec Teal

Reputation: 5918

Try making d const, if you've got C++11 use constexpr, the compiler is upset because there's no reason d can't change at any time. It treats T[N] as a type you see (this is useful for optimisations) it's upset because your Btrees may not be all the same.

You could add an int template param to your Btree by the way, then it'd be happy (given that integer was a constexpr) because all the things from that template would be the same, but you couldn't mix them (Btree<1> and Btree<2> wouldn't be able to interact, 'cept through a function that explicitly (via a template or otherwise) used them both)

If d can change, you really want that on the heap.

Upvotes: 1

Related Questions