How do you create a private dynamic array in a c++ class?

11,845

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>.

Share:
11,845
nszejna
Author by

nszejna

Updated on June 18, 2022

Comments

  • nszejna
    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 years
      Is this homework or excercise? Otherwise I would simply recommend using std::vector<double> instead of providing your handrolled class.
    • nszejna
      nszejna over 11 years
      Yes 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
      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
      andre over 11 years
      The code should be double* index_pointer; no '*index_pointer = new double[100];'
  • Jacob
    Jacob over 11 years
    this is obviously homework to implement a naive vector<double>, so I doubt that's an option
  • πάντα ῥεῖ
    πάντα ῥεῖ over 11 years
    In 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
    matthew3r over 11 years
    Maybe, 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
    nszejna over 11 years
    Oh 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_pointerin the 1st place. Creating the array dynamically happens in the constructor code, yes.