parhloo sab kuch
parhloo sab kuch

Reputation: 17

how to fix Error in initializing an array using a variable?

I am trying to implement a stack using an array. This is what I have in my header file.I plan to assign the value to maxsize in the constructor. However, i keep getting errors. How can i fix this?

class stack {


 private:

const int maxsize;
int arrays[maxsize];
int top;

public:

stack();
void additem(int);
void print();
};

Upvotes: 0

Views: 152

Answers (3)

M.M
M.M

Reputation: 141628

A good fix would be:

class stack
{
    std::vector<int> arrays;
    int top;

public:
    stack(int maxsize) : arrays(maxsize), top(0) {}
};

This way, you do not have any possibility of memory management bugs; your class behaves properly when copied, moved, swapped, etc. and your code is very simple.

An alternative, with a minimal memory footprint would be:

class stack
{
    std::unique_ptr<int[]> arrays;
    int maxsize;
    int top;

public:
    stack(int maxsize) : arrays(new int[maxsize]), maxsize(maxsize), top(0) {}
};

This version is movable, but will give compile errors when copied (as opposed to some of the other suggestions to use raw pointer, which will compile successfully and then give memory corruption at runtime). To make this class copyable you'd need to write your own copy-constructor and copy-assignment operator.

Upvotes: 1

Miles Budnek
Miles Budnek

Reputation: 30579

If you want maxsize to always be 10, you could do something like this:

class stack {
private:
    static const int maxsize = 10;
    int arrays[maxsize];
...

Note that I've made maxsize static. It will be the same for all instances of stack and is defined at compile time. Thus you can use it as an array size.

If you want maxsize to be variable, then you'll need to use dynamic allocation:

class stack {
private:
    const int maxsize;
    int* arrays;
    ...
public:
    stack(int maxsize) : maxsize(maxsize), arrays(0) {
        arrays = new int[maxsize];
    }
    ~stack() { delete[] arrays; }
    ...

Note here that you must delete the memory you dynamically allocate. The usual place to do that is the destructor. If you want to go this route, you could also just use a std::vector<int> and get rid of maxsize entirely. That frees you from having to do any manual memory management.

Upvotes: 0

Barmar
Barmar

Reputation: 781741

C++ doesn't allow variable-length arrays. Instead of an array, you can use a pointer and dynamic allocation.

And to initialize a const member, you have to do it in the initializer list of the constructor.

class stack {

private:
    const int maxsize;
    int *arrays;
    int top;

public:
    stack(int max = 10) : maxsize(max) {
        arrays = new int[max];
        top = 0;
    }
    void additem(int);
    void print();
};

See How to initialize a const field in constructor?

You'll also then need a destructor that does delete[] arrays;.

But rather than using a C-style array, you'd probably find it easier to use std::vector<int>. This doesn't require specifying a maximum length in the first place, it will expand as needed.

Of course, you could skip this whole exersize and use std::stack<int>.

Upvotes: 0

Related Questions