Dynamic Array

Hello
I'm getting an error in the implementation of my push back function to append values to a dynamic array of floating points type double

The function should take as an argument a double, and should return void.

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
#ifndef DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_H

class Dynamic_Array {

public:
	//Data fields

	//Default Constructor
	Dynamic_Array();

	//Copy Constructor
	Dynamic_Array(const Dynamic_Array&);

	//Destructor
	~Dynamic_Array();

	//Assignment Operator
	const Dynamic_Array& operator = (const Dynamic_Array&);

	//Subscript Operator []
	double& operator [](size_t); 
	const double& operator [] (size_t) const; 

	//Class Member Functions

	void push_back(double);
	void pop_back();
	double size();
	bool empty();

private:
	//Data fields

	static const double DEFAULT_CAPACITY;
	double capacity;
	double number_of_items;
	double* data;

	//Private Class- Member Function
	void resize();

};

#endif 

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
#include "Dynamic_Array.h"
#include <stddef.h>
#include <cmath>
using namespace std;


double calculateSD(double arr[]){

	double sum = 0.0, mean, standardDeviation = 0.0;

	for (size_t i = 0; i < 10; ++i){

		sum += arr[i];
	}

	mean = sum / 10;

	for (size_t i = 0; i < 10; ++i) {

		standardDeviation += pow(arr[i] - mean, 2);
	}
	
	return sqrt(standardDeviation / 10);
}

/** Evaluates whether the array is empty or not
	@return: {TRUE} if the array is empty
	@return: {FALSE} if there are numbers stored the array */

bool Dynamic_Array::empty() { return size() == 0; }


double Dynamic_Array::size() { return number_of_items; }


/** Removes the last element ot the array */
void Dynamic_Array::pop_back() { number_of_items--; }

/** Function push_back()
	@param value: New element appended */
void Dynamic_Array::push_back(double value) {

	if (number_of_items == capacity) { resize(); }

	data[number_of_items++] = value; //Im getting the error here under 
}

/**Doubles the "capacity" of the array, without changing its current values */
void Dynamic_Array::resize() {

	capacity *= 2;

	double* newArr = new double[capacity];

	for (size_t i = 0; i < number_of_items; i++) { newArr[i] = data[i]; }

	delete[]data;

	data = newArr;
}


//Overloading subscript operator 
double& Dynamic_Array:: operator [] (size_t index) { return data[index]; } //lvalue 

const double& Dynamic_Array::operator [] (size_t index)const { return data[index]; } //rvalue

//Deconstructor
Dynamic_Array::~Dynamic_Array() {  //Delete all the dynamic allocated memory
	if (data != NULL) { delete data; }
}

//Copy Constructor
Dynamic_Array::Dynamic_Array(const Dynamic_Array& other) {
	data = NULL;
	*this = other;

}

//Deep Copy Assignment Operator
const Dynamic_Array& Dynamic_Array::operator= (const Dynamic_Array& rhs) {

	if (this != &rhs) { //Avoiding self-assignment

		if (data != NULL) {
			delete[] data; //Deleting dynamically allocated memory
			data = NULL;
		}
		//Copying static data
		number_of_items = rhs.number_of_items;
		capacity = rhs.capacity;

		if (capacity > 0) {
			//Copying dynamic data
			data = new double[capacity];

			for (size_t i = 0; i < number_of_items; i++) {
				if (i < capacity) {
					data[i] = rhs.data[i];
				}
			}
		}
	}
	return *this; //Return
}

//Default Constructor
Dynamic_Array::Dynamic_Array() : capacity(DEFAULT_CAPACITY), number_of_items(0) { //Setting capacity wiht DEFAULT_CAPACITY and num_of_items with 0

	data = new double[capacity]; //Initialize data to a dynamic array of size capacity

}

const double Dynamic_Array::DEFAULT_CAPACITY = 10; //Set DEFAULT_CAPACITY to 1 


The error on Visual Studio says " E2140 expression must have integral or unscoped enum type"
Any suggestions on how to fix it?
Last edited on
You haven't shown enough code to reliably identify the problem.
I have add the whole code now
The size, capacity, and default capacity need to be represented exactly - as variables of type size_t. Floating point types like double are just approximations of some real numbers.
static const unsigned int DEFAULT_CAPACITY;
unsigned int capacity;
unsigned int number_of_items;
double* data;

Okay, I just made this changes and the error disappeared. So capacity and number_of_items don't need to be doubles?
Given
double data[] = { 0.5, 1.5, 2.5, 3.5; };
and
double number_of_items = 1.25;
What should data[number_of_items] mean?

The folks who designed C considered this question and decided that the compiler should reject this code - presumably because it is likely wrong.

Doubles are not simple - they're full of corner cases. For example, it is typically possible to find values x, y, u, v, w for which x + 1 == x, and y != y, and (u + v) + w != u + (v + w).

Makes sense! Thank you!
Topic archived. No new replies allowed.