How do you create a private dynamic array in a c++ class?
Solution 1
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.
Solution 2
You should use std::vector<double>
.
nszejna
Updated on June 18, 2022Comments
-
nszejna almost 2 years
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*'
-
πάντα ῥεῖ over 11 yearsIs this homework or excercise? Otherwise I would simply recommend using
std::vector<double>
instead of providing your handrolled class. -
nszejna over 11 yearsYes it is homework, I have learned how to use vectors, but we are supposed to use dynamic arrays to make a class similar to vectors but with the type double only.
-
leemes over 11 years@g-makulik "I'm instructed that..." sounds like homework. So I think they are learning RAII in C++, including writing a copy constructor and stuff.
-
andre over 11 yearsThe code should be
double* index_pointer;
no '*index_pointer = new double[100];'
-
Jacob over 11 yearsthis is obviously homework to implement a naive
vector<double>
, so I doubt that's an option -
πάντα ῥεῖ over 11 yearsIn case you need to supply this class as surrogate for
std::vector<double>
because you can't use STL in your environment or simply asked to implement this as an excercise, this answer will probably solve your problem. -
matthew3r over 11 yearsMaybe, but he never said that. If not an option, then he will not use it :) - Now I see it's a homework, but otherwise
vector
is the answer. -
nszejna over 11 yearsOh so only the pointer is in the private section then the part where the array is created is in the constructor?
-
πάντα ῥεῖ over 11 years@user1678621 No. The problem is that you'll have to declare
index_pointer
in the 1st place. Creating the array dynamically happens in the constructor code, yes.