0

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;
};
2
  • Variable length arrays are not allowed in C++ (but they are in C, and some compilers have extensions to support them). As it's defined, d is a variable. Commented Sep 26, 2013 at 0:30
  • Add a comment to my answer if you need more info, it's always good to upvote if you approve, if it were sufficient. If it's right but lacking, that's just a tick. Commented Sep 26, 2013 at 0:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.