So I have this line:
ArrayList<Operation> operations();
Now, this is my ArrayList class and my Operation struct:
typedef struct Operation {
char key;
int value;
} Operation;
template <class DT>
class ArrayList
{
private:
int _capacity;
int _size;
DT* elements;
public:
ArrayList();
~ArrayList();
void insert (DT&);
DT& operator[] (int);
};
ArrayList<DT>::ArrayList()
{
_capacity = 10;
_size = 0;
elements = new DT[10];
}
I don’t see the need of posting the code of the other methods since the error isn’t happening there. However if you want to see them you just have to ask.
Now, everytime I try to do something like
operations.insert(x) //assuming x is a struct that exists.
or
operations[i].key; //assuming i is a declared and initialized index.
It gives me the error C2228: left of (fill in the blank) must have class/struct/union
and error C2109: subscript requires array or pointer type
I have seen a previous thread about this problem and I think my problem is that the compiler takes the first line of code I provided as a declaration, but no initialization.
However I didn’t see a solution for this. In my head the only solution is to make it a pointer and use = new ... but in my head the keyword new is synonym of nastiness so I simply wanted to make it a an object. What is a way to go around this?
Or perhaps I am wrong, and this has something to do with the object type being a struct since this is the first time I work with structs.
This line:
Declares a function that returns an
ArrayList<Operation>This is commonly referred to as most vexing parse.
To declare your
ArrayList<Operation>, remove the parenthesis: