Initializing arrays in constructors

I have two constructors: an explicit default ctor and a value ctor that will be used to create a histogram.

Whenever I try to compile my value ctor -- specifically focusing on the putSamples[] -- I get an error reading "uninitialized member ‘SamplingClass::putSamples’ with ‘const’ type ‘const int [0]’ [-fpermissive]
SamplingClass::SamplingClass()"

In this case, is there way to initialize the array in the value ctor, or is it necessary to define it outside of the ctor? Any help is appreciated.

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
class SamplingClass
{
    public:
    SamplingClass();
    SamplingClass(const char inIdChar, const int inNumSamples, 
                      const int inputSamples[]);
    private:
    char IdChar;
    int NumSamples;
    const int putSamples[];
    bool readFromKeyboard();
    bool printToScreen();
};

SamplingClass::SamplingClass()
{
    IdChar = 0;
    NumSamples = 0;
    putSamples[99] = {0};
    cout << "Does this work?" << endl;
};


SamplingClass::SamplingClass(const char inIdChar, const int inNumSamples, const int inputSamples[])
{
    IdChar = inIdChar;
    NumSamples = inNumSamples;
    putSamples = inputSamples;
    
    if (NumSamples > MAX_DATA_SET_SIZE)
    {
        cout << "You have exceeded the allowable amount of samples supported by this class" << endl;
        IdChar = 0;
        NumSamples = 0;
        putSamples[99] = {0};
    }
}
Last edited on
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
class object
{
    public:
      object(short int inArr[],const int & s):
      size(s),
      outArr(new short int[s]) 
       //make the class array the same size as the given one
      {
          for(int unsigned i=0; i<s;++i)
            outArr[i]=inArr[i]; //copy contents
      };
       
     friend std::ostream & operator<<(std::ostream & op, const object & someObj) 
         //just for test printing
     {
          for(int unsigned i=0; i<someObj.size;++i)
            op << someObj.outArr[i] << char(0x20);   
          return op;
     };

    private:
         short int size;
         short int  * outArr;
};


int main()
{
  int short arr[]={1,3,4,5};
  object obj(arr,sizeof(arr)/sizeof(arr[0]));
  
  std::cout<<obj;
  
  return EXIT_SUCCESS;
}


Output:
1 3 4 5  
Last edited on
You need to specify the size of the array, and it has to be a compile time constant. You probably also don't want to use const because it makes it impossible to copy values to it.

 
int putSamples[MAX_DATA_SET_SIZE];

This line ...
 
putSamples[99] = {0};
... does not change the size of the array. In fact, it is impossible to change the size of an array after it has been created. What this line actually does is to set the array element at index 99 to zero.

You can also not copy arrays using the = operator. Instead you would have to use a loop or a function such as std::fill.

A better choice might be to use std::vector instead of arrays. std::vector can easily be resized and you can easily copy them using the = operator.
http://www.cplusplus.com/reference/vector/vector/
Last edited on
Golden Lizard wrote:
sizeof(inArr)/sizeof(inArr[0])

This will not give you the length of the array. inArr is a pointer so sizeof(inArr) will give you the size of the pointer.
Thanks for pointing out, fixed it.
Topic archived. No new replies allowed.