Dynamic Array Of Dynamic Objects

I keep getting all kinds of errors, I have an assignment where I need to convert a Java program into c++ and the assignment's requirement is I need to have a dynamic array of dynamic objects. Some of the errors I'm getting are:
"19 `new' cannot appear in a constant-expression "
"19 ISO C++ forbids declaration of `list' with no type "
"19 ISO C++ forbids initialization of member `list' "
"19 making `list' static "
"19 ISO C++ forbids in-class initialization of non-const static member `list' "
"19 declaration of `int Vector::list'" Below are the two of the classes of this program any help or suggestions would be appreciated :)

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
#pragma once
#include <cstdlib>
#include <iostream>
#include <string>
//#include "Database.h"
#include "Person.h"
#include "Student.h"
/*#ifndef "Student.h"
#define "Student.h"
using namespace std;*/
class Vector
{
    // In CPP, you have to create a DYNAMIC array of DYNAMIC objects.
    // Otherwise, TA will deduct 35 points.
    private:
            int capacity; //capacity of the current array
            int size;     // number of current records
            Student *list;
            list = new Student[10];
    public:
           Vector();
           ~Vector();
           int getCapacity();
           int getSize();
           int getIndex(int ID);
           Student get(int index);
           void print();
           bool add(Student newStudent);
           int remove(int productID);
};

    
    Vector::Vector()
    {
        // the initial array will have 2 elements
        capacity =2;
        size = 0;
        list = new Student[capacity];
    }
    int Vector::getCapacity()
    {
        return capacity;
    }
    int Vector::getSize()
    {
        return size;
    }

    int Vector::getIndex(int ID)
    {
         int index = -1;
         for(int i=0; i<size; i++)
         {
            if( list[i].getID() == ID )
            {
                index = i;
            }
         }
         return index;
    }

     Student Vector::get(int index)
     {
         return list[index];
     }

     void Vector::print()
     {
         for(int i=0; i<size; i++)
         {
             //System.out.println( list[i].toString() );
             cout<<list[i].toString();
         }
     }


    //sonra
    bool Vector::add(Student newStudent)
    {
        bool result = true;
        int index = getIndex( newStudent.getID());
        if( index == -1 )
        {
            if( size < capacity)
            {
                list[size] = newStudent;
                size++;
            }
            else
            {
                //GROW
                capacity = capacity*2;
                Student[] tmpList = new Student[capacity];
                for(int i=0; i<size;i++)
                {
                    tmpList[i] = list[i];
                }
                list = tmpList;
                list[size] = newStudent;
                size++;
            }
        }
        else
        {
            result = false;
        }
        return result;

    }

    int Vector::remove(int productID)
    {
        int result = 1;
        int index = getIndex( productID);
        if( index == -1)
        {
            result = index;
        }
        else
        {
           for(int i=index; i<size-1; i++)
            {
                list[i] = list[i+1];
            }
            size = size-1;
            // SHRINK
            if( size*2 == capacity )
            {
                capacity = capacity/2;
                Student[] tmpList = new Student[capacity];
                for(int i=0; i<size; i++)
                {
                    tmpList[i] =  list[i];
                }
                list = tmpList;
                size = capacity;
            }

        }
        return result;
    }

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
#pragma once
#include <cstdlib>
#include <iostream>
#include <string>
//#include "Database.h"

#include "Person.h"
//#ifndef "Person.h"
//#define "Person.h"
//#include "Vector.h"
using namespace std;
class Student: public Person
{
    private:
            string major;
            int    collegeYear; // 1 freshman 2 sophomore 3 junior 4 senior
    public:
           Student(int ID, string firstName, string lastName, string major, int collegeYear);
};

Student::Student(int ID, string firstName, string lastName, string major,int collegeYear):Person(ID, firstName, lastName), major(major), collegeYear(collegeYear)
    {
        Person(ID, firstName, lastName);
        this->major       = major;
        this->collegeYear = collegeYear;
    }
This ones easy, they all pretty much say "Delete Line 19". You already have the variable declared at 18, and there is an allocator in your constructor so you aren't losing anything. You can't declare any constants like the size of an array in the way that you have it, judging by the rest of your code you don't want to do that anyway.
Cool, I made my first dynamic array today, funnily enough (quite new to C++). I had it set up with void pointers so it stores pretty much anything, and then decided to add a dynamic array for usage and an ID and now it pretty much handles all the dynamic allocation and smart pointers in my program.

In your grow and shrink sections you use temporary lists to transfer information to the newly resized array space, but I don't think they're being deallocated, are you sure you don't need to "delete[] tmpList;"?

And maybe you should add a minimum size for the array in the shrink section.

And when growing you are creating a temporary array that is twice as big as it needs to be, let it be the original size as it only needs to store the data from the old small array to transfer to the new big one.

Forgive me if I'm mistaken anywhere, since as I said I'm quite new to C++.
Veltas, yes I do need to add delete[] and some other things, I'm just trying to get the thing compiled lol then I'll go look at logic errors and such, I think I am close but now I keep getting " [Linker error] undefined reference to `Vector::~Vector()' " error message, does anyone know why?
Yes, you haven't defined ~Vector(), the Vector class destructor. You must either define it or remove the reference in the class.
Ok sorry I had changed the code since then, it now reads

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
class Vector
{
    // In CPP, you have to create a DYNAMIC array of DYNAMIC objects.
    // Otherwise, TA will deduct 35 points.
    private:
            int capacity; //capacity of the current array
            int size;     // number of current records
            Student *list;
            //list = new Student[10];
    public:
           Vector();
           ~Vector(){}
           int getCapacity();
           int getSize();
           int getIndex(int ID);
           Student get(int index);
           void print();
           bool add(Student newStudent);
           int remove(int productID);
};

    Vector::Vector()
    {
        // the initial array will have 2 elements
        capacity =2;
        size = 0;
        list = new Student[capacity];
    }


but I've also tried:

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
class Vector
{
    // In CPP, you have to create a DYNAMIC array of DYNAMIC objects.
    // Otherwise, TA will deduct 35 points.
    private:
            int capacity; //capacity of the current array
            int size;     // number of current records
            Student *list;
            //list = new Student[10];
    public:
           Vector()
         {
         // the initial array will have 2 elements
         capacity =2;
         size = 0;
         list = new Student[capacity];
}
           ~Vector(){}
           int getCapacity();
           int getSize();
           int getIndex(int ID);
           Student get(int index);
           void print();
           bool add(Student newStudent);
           int remove(int productID);
};


and like this:


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
class Vector
{
    // In CPP, you have to create a DYNAMIC array of DYNAMIC objects.
    // Otherwise, TA will deduct 35 points.
    private:
            int capacity; //capacity of the current array
            int size;     // number of current records
            Student *list;
            //list = new Student[10];
    public:
           Vector();
           ~Vector();
           int getCapacity();
           int getSize();
           int getIndex(int ID);
           Student get(int index);
           void print();
           bool add(Student newStudent);
           int remove(int productID);
};
Vector::~Vector(){}
Vector::Vector()
         {
         // the initial array will have 2 elements
         capacity =2;
         size = 0;
         list = new Student[capacity];}



and none of them are working for some reason...
Ok so I was screwing around in my compiler Dev-C++ and stumbled upon a rebuild all button and then all of a sudden there are no errors whatsoever and it runs just fine.... has anyone ever heard of this happening? or why even?
Sorry, no idea.

Glad to hear it worked at least! Now promise you'll add a couple of delete[]s!
workin on it, haha, thanks for everyone's help!
Topic archived. No new replies allowed.