Default Constructor issue

I have a class whose Default Constructor is giving me issues. I am trying to make it so that, when the class object is instantiated, it will have only an array of four doubles set to 0, 0, 0, 0. How do I go about doing this? I have, so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// .h file

#pragma
#ifndef BLAHSALES_H
#define BLAHSALES_H

class BlahSales
{
private:
	double quarterSales[];
public:
	BlahSales();
};

#endif 


1
2
3
4
5
6
7
8
// .cpp file

#include "BlahSales.h"

BlahSales::BlahSales()
{
	quarterSales[]={0, 0, 0, 0};
}


The first curly brace in the default constructor in the .cpp file says that it "expects an expression." Does this mean I have to give the array a set size? If so, where?

1
2
private:
        double array[]; // HERE? 


as in... ?

1
2
3
4
5
6
7
8
9
10
// .h
private:
        const int SIZE = 4;
        double array[SIZE];

// .cpp Default constructor
BlahSales::BlahSales()
{
     array[SIZE] = {0, 0, 0, 0};
}


If so, is it bad practice to give it (my class) a const int attribute, even if I know I'm always going to have 4 values for the array?
Last edited on
Have you tried using the constructor's initializer list? By the time you reach line 7 in your first example the array has already been created and initialized.

You may want to note that regardless of that, your attempts to assign to an array on line 7 of the first and 9 of the second examples aren't valid syntax; in the first you can't assign to a non-index element of an array and in the second you are not only trying to assign to an element 1 unit past the end of the array but also trying to assign a list to it.
I hadn't realized/didn't know that quarterSales[]={0, 0, 0, 0}; and array[SIZE] = {0, 0, 0, 0}; were improper syntax. How disheartening. Anywho, this is what I've come up with knowing that fact. No errors (as far as the compiler tells me).
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma
#ifndef BLAHSALES_H
#define BLAHSALES_H

class BlahSales
{
private:
	double quarterSales[4];
public:
	BlahSales(double thisArray[]);
};

#endif 


And then...

1
2
3
4
5
6
7
8
9
10
11
12
#include "BlahSales.h"

/**********************************
*       Default Constructor       *
***********************************/
BlahSales::BlahSales(double thisArray[])
{
	for(int i = 3; i >= 0; i--)
	{
		quarterSales[i] = thisArray[i];
	}
}
Last edited on
Topic archived. No new replies allowed.