invalid use of non-static data member

I wanna to use the code given below:

sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <stdio.h>
class test
{
  public:
  test()
  {
    sample = 1;
  }
  private:
  int sample;
  int *array[sample];
};
int main()
{
  test a;
  return 1;
}


The variable sample = 1 is to be assigned to the array of pointers *array[sample]. But when I compile, I get the following error:
test1.cpp:10: error: invalid use of non-static data member âtest::sampleâ
test1.cpp:11: error: from this location
test1.cpp:11: error: array bound is not an integer constant

Kindly help me.!!

Thanks in Advance.
Last edited on
> array bound is not an integer constant
focus on that message.
>array bound is not an integer constant

What to do for this.?
the size of the array must be know at compile time.
Also, all `test' objects should report the same sizeof

int *array[1]; would work, ¿but what are you trying to do?
Actually, I wanna to create an array of pointers with size of a variable which is passed from the constructor. In this case, sample is the varaible passed from constructor and *array[sample] is the array of pointers.

The above code is just stimulation of my actual problem.
1
2
3
4
5
class foo{
   std::vector<int*> v;
public:
   foo(int size): v(size){}
};
Topic archived. No new replies allowed.