Hi I am pretty new to C++ I’m a lot more used to Java. But what I want to do is create just an empty arrayList that can be used by all of my other functions.
In my .h file I created a new class which has 2 structs within it and about 6 methods. Each of these methods uses arrayLists of the struct objects, but I don’t want to have to pass in the arrayLists as parameters. What I am trying to do is similar to in Java when you create a field in a class.
The problem is when I declare the arrayList like this in my .h file…
structObject * myArrayList = new structObject[0];
int ARRAY_SIZE = arrayList.size;
I get a bunch of errors including new cannot be declared in a constant.
Where should I be trying to declare my empty arraylist?
Is this even permissible in C++?
You are trying to put a definition in a header file. That is a not allowed. You can only put declarations in a header file.
declaration in the header:
definition in a source file:
However, global variables are almost always a bad idea. I would find a way to pass the array as an argument to the necessary functions. You should also look into the Standard Template Library and look at different containers.
one option:
That can be put in a header file, and the constructor will do necessary initialization.