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 :)
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?
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];
}
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);
};
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];}
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?