Error zero size array, working with classes.

Keep getting an error saying I have zero sized array but I have a constructor creating an array of 50. Also getting an error that says: deletion of an array expression; conversion to pointer supplied . As well I keep getting errors: Unhandled exception at 0x0009A835 in Stat_list_32.exe: 0xC0000005: Access violation reading location 0x00000060.

Any ideas might help. I will provide implementation file, header file, and the client code.

This is my implementation file:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include"stat_list.h"
using namespace std;

		double mean;
		double median;
		bool statList::isEmpty() const
		{
			return (length == 0);
		}
		bool statList::isFull() const
		{
			return (length == maxSize);
		}
		int statList::getLength() const
		{
			return length;
		}
		
		void statList::printLength(int& newLength){
			cout << "The length of your array is: " << newLength << endl;
		}
		int statList::getMaxSize() const
		{
			return maxSize;
		}
		double statList::getMean() const                            /* Clean me up */
		{
			double sum = 0;
			double mean;
			for (int a = 0; a < length; a++){
				sum += list[a];
			}
			mean = (sum / length);
			cout << "The mean of the array of doubles: " << mean << "\n" << endl;
			return mean;
		}//end getMean
		double statList::getMedian() const {
			int m1, m2;
			double median;
			m1 = (1 / 2)*(length);
			m2 = m1 + 1;
			median = (list[m1] + list[m2]) / 2;
			cout << "The median of the array is: " << median << "\n" << endl;
			return median;
		}//end getMedian
		double statList::getStdDev() const  /* *WRITTEN* */
		{
			double stdDev;
			double stdTemp = 0;
			for (int i = 0; i < length; i++)
			{
				stdTemp += pow((list[i] - mean), 2);
			}
			stdTemp = stdTemp / length;
			stdDev = sqrt(stdTemp);
			cout << "The Standard Deviation: " << stdDev << endl;
			return stdDev;
		}

		//Constructor; the default array size is 50
		statList::statList(int listSize)
		{
			int i;
			maxSize = listSize;
			length = 0;
			for (i = 0; i < maxSize; i++) list[i] = 0;
		}
		statList::~statList()  //destructor
		{
			delete[] list;
		}

		//selection sort
		void statList::readInList(ifstream& infile)  /* *WRITTEN* */
		{
			infile >> list[0];
			while (infile && (length < maxSize)){
				length++;
				infile >> list[length];
			}
		}//end readInList
		void statList::insertAt(const double& item1, int position1)   /* Need New Length */
		{
			assert(position1 >= 0 && position1 < maxSize);
			do{
				double temp = list[position1];
				list[position1] = item1;
				
			} while (position1 >= 0 && position1 < maxSize);
			length++;
		}//end insertAt
		void statList::find_index(const double& item){
			int position = 0;
			while (list[position] < item){
				position++;
			}
			insertAt(item, position);

		}//end find_index
		void statList::insertInOrder(const double& newItem)       /* ***WRITTEN*** */
		{
			find_index(newItem);
		}
		void statList::sort()
		{
			int min;
			double temp;
			for (int i = 0; i < length - 1; i++)
			{
				min = i;
				for (int j = i + 1; j < length; j++)
				{
					if (list[j] < list[min])
						min = j;
				}
				if (min != i)
				{
					temp = list[i];
					list[i] = list[min];
					list[min] = temp;
				}
			}
		}//end sort
		void statList::print() const   /* Print Array */
		{
			int i;
			for (i = 0; i < length; ++i)
				cout << list[i] << " ";
			cout << endl;
		}//end print

		void statList::unsortList()  /* *WRITTEN* */
		{
			int i, offset;
			double temp;
			srand(time(NULL));
			for (i = 4; i < length; i += 2){
				offset = rand() % 4;
				temp = list[i - offset];
				list[i - offset] = list[i];
				list[i] = temp;
			}
			for (i = length / 6; i < length; i += 4){
				offset = rand() % (length / 6 - 1);
				temp = list[i - offset];
				list[i - offset] = list[i];
				list[i] = temp;
			}
		}


This is the class/header file.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#ifndef H_statList 
#define H_statList

#include <iostream>
#include <cassert>
#include <cstdlib>
 
using namespace std;


	class statList
	{
	public:


		bool isEmpty() const;  /* *WRITTEN* */
		//Function to determine whether the list is empty.
		//Postcondition: Returns true if the list is empty,
		//               otherwise it returns false.

		bool isFull() const;  /* *WRITTEN* */
		//Function to determine whether the list is full.
		//Postcondition: Returns true if the list is full,
		//               otherwise it returns false.
		

		int getLength()const;  /* *WRITTEN* */
		//Function to return the number of elements in the list.
		//Postcondition: The value of length is returned.

		int getMaxSize() const;  /* *WRITTEN* */
		//Function to return the maximum number of elements 
		//that can be stored in the list.
		//Postcondition: The value of maxSize is returned.

		void sort();  /* *WRITTEN* */
		//Function to sort the list.
		//Postcondition: The list elements are in ascending order.

		void print() const;  /* *WRITTEN* */
		//Outputs the elements of the list.

		void insertAt(const double& item, int position);  /* *WRITTEN* */
		//Function to insert item in the list at the location
		//specified by position.
		//Postcondition: list[position] = item; length++;
		//               If position is out of range, the program
		//               is aborted.

		void insertInOrder(const double& item);  /* *WRITTEN* */
		//Function to insert item in the list at the location
		//found to be appropriate by scanning the array.
		//Use insertAt() once the position is found.
		//Postcondition: list[position] = item; length++;
		//               If position is out of range, the program
		//               is aborted.

		double getMean() const;  /* *WRITTEN* */
		//Function to sum the list, then divide the sum by the length
		//to calcukate and store the mean.
		//Postcondition:  mean is stored.

		double getMedian() const;  /* *WRITTEN* */
		//Function to sort the list, then find the median by finding 
		//the middle element. If the number of list items is even,
		//then the mean of the two middle items is calculated.
		//Postcondition:  median is stored.

		double getStdDev() const;  /* *WRITTEN* */
		//Function to getnthe mean, using getMean(), and use the 
		//following definition of Sample Standard Deviation to compute
		//it via a for loop.
		//Definition:  Standard Deviation = 
		//sqrt((1/N)sum[1:N]((x[i]-average)*(x[i]-average)))
		/*HINT:  the summation is accomplished by the for loop.*
		 *So sum the square of the differences, then, after the*
		 *loop is executed, divide and take the square root.   */
		//Postcondition:  sample standard deviation is stored.

		void readInList(ifstream& infile);  /* *WRITTEN* */
		//Function to read items into the list.
		//A designated input file will be read to EOF, and the values 
		//stored in the list for later analysis.
		//Postcondition:  The list is populated.
		void find_index(const double& item);
		void printLength(int& newLength);
		void unsortList();  /* *WRITTEN* */
		//test function to put the list in an unordsered state for test purposes.
		//Postcondition:  The list has the same elememnts, but is unsorted.

		statList(int listSize=50);  /* *WRITTEN* */
		//Constructor
		//Creates an array of double of the size specified by the
		//parameter listSize; the default array size is 50.
		//Postcondition: list contains the base address of the
		//               array; length = 0; maxsize = listSize;
		//               the array is initialized.

		~statList();  /* *WRITTEN* */
		//Destructor
		//Deletes all the elements of the list.
		//Postcondition: The array list is deleted.

	private:
		int maxSize;   //variable to store the maximum size 
		//of the list
		int length;    //variable to store the number of elements
		//in the list
		double list[]; //pointer to the array that holds the
		//list elements

	};

#endif 

double list[]; //pointer to the array
You are not declaring a pointer here, you're declaring an array without a known size, which is illegal.
double* list; declares a pointer

You delete[] your list in your destructor, but you never allocated it with new[].

Your constructor should be something along the lines of this:
1
2
3
4
5
6
statList::statList(int listSize)
{
    maxSize = listSize;
    length = 0;
    list = new double[listSize](); // the () makes each element initialize to 0
}
Last edited on

Thanks Ganado, I've been at this for days. This is my first attempt at classes. I really appreciate it.
Glad to help!
Topic archived. No new replies allowed.