ISO c++ forbids initialization of member
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <stdlib.h>
class IntStack
{
int INITIAL_SIZE = 100;
int i;
int* st_arr = new int[INITIAL_SIZE];
public:
void reset();
void push(int n);
void push(int a[], size_t array_size);
int pop();
void pop(int a[], size_t n);
int top();
int size();
bool is_full();
bool is_empty();
friend IntStack print_stack(IntStack* pstack);
};
|
Is there any way to initialize INITIAL_SIZE in this class without using a different source file?
ISO c++ forbids initialization of member
it fobids because your compiler does not support C++0x future for initializing class members.
use constructors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <stdlib.h>
class IntStack
{
int INITIAL_SIZE;
int i;
int* st_arr;
public:
//DEFAULT CONSTRUCTOR
IntStack() : INITIAL_SIZE(100), st_arr(new int [INITIAL_SIZE]) {}
//OVERLOADED CONSTRUCTOR
explicit IntStack(int num) : INITIAL_SIZE(num), st_arr(new int [INITIAL_SIZE]) {}
//DESTRUCTOR
~IntStack() { delete [] st_arr;}
//COPY CONSTRUCTOR
IntStack(const IntStack& ref) : INITIAL_SIZE(ref.getInit()), st_arr(new int [INITIAL_SIZE]) {
for(int i = 0; i < INITIAL_SIZE; ++i)
st_arr[i] = ref.get_st_array(i);
}
//MOVE CONSTRUCTOR
IntStack(IntStack&& ref) : INITIAL_SIZE(ref.INITIAL_SIZE), st_arr(ref.st_arr) {
st_arr = nullptr;
i = 0;
INITIAL_SIZE = 0;
}
//METODS USED BY CONSTRUCTORS
int get_st_array(int x) const {
return st_arr[x];
}
int getInit() const {
return INITIAL_SIZE;
};
//your code...
void reset();
void push(int n);
void push(int a[], size_t array_size);
int pop();
void pop(int a[], size_t n);
int top();
int size();
bool is_full();
bool is_empty();
friend IntStack print_stack(IntStack* pstack);
};
|
Last edited on
Topic archived. No new replies allowed.