Hello I am having trouble with using dynamic arrays within a class. I am instructed that "the class VectorDouble will have a private member variable for a dynamic array of doubles." I am only as far as writing the header file for this program, but I have not gotten past that. This class needs to be able to double in size once it has reached capacity. Here is my code:
#include <iostream>
using namespace std;
// VectorDouble class header file
class VectorDouble
{
public:
// constructor for an empty vector of type double
VectorDouble();
// constructor that take an int argument for an integer size of and array
VectorDouble(int initial_size);
// copy constructor
VectorDouble(VectorDouble &object);
// destructor to return dynamic memory to the freestore
~VectorDouble();
// overloaded operator = for VectorDouble class
VectorDouble& operator =(const VectorDouble &source);
// overloaded operator == for VectorDouble class
friend bool& operator ==(const VectorDouble &this_vector,
VectorDouble &other_vector);
// adds a new element at the end of the array
void push_back();
// returns allocated size of VectorDouble dynamic array
int capacity();
// returns used size of the VectorDouble object
int size();
// allocates a specified block of memory for specified number of double
// type values
void reserve(int new_reserve);
// changes the size of the dynamic array
void resize(int new_size);
// returns the value at the specified index
double value_at(int index);
// changes the value at the specified index to double value d
void change_value_at(double d, int index);
private:
int count;
int max_count;
int index_pointer;
*index_pointer = new double[100];
};
The errors I am getting are all on this line:*index_pointer = new double[100];
`new' cannot appear in a constant-expression
ISO C++ forbids declaration of `index_pointer' with no type
ISO C++ forbids initialization of member `index_pointer'
making `index_pointer' static
invalid in-class initialization of static data member of non-integral type `int*'
Your pointer needs a type. Change that line to
double* index_pointer;
And in your constructor add the line
index_pointer = new double[100];
And so on for your other constructors and assignment operator.
But this is also a naming conflict because you have another private int member named index_pointer. I'm not sure what that member is for, but if you do actually need it then you'll have to name it or the pointer something else.
Remember to delete[] index_pointer;
in your destructor.
You should use std::vector<double>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With