Reputation: 55
I'm trying to program my own implementation of a map class using templates and and not using vectors, only an array.
I have two classes: Element which stores the Key and the data.
template <typename K, typename DT>
class Element
{
public:
K key;
DT data;
};
And the Map class.
Private Variables:
template <typename K, typename DT>
class myMap
{
typedef Element<K, DT> myElement;
private:
myElement *elements;
int size;
Constructor:
Template <typename K, typename DT>
myMap<K, DT>::myMap()
{
//An initial map with a size of 10 potential elements.
size = 10;
elements = new myElement[size];
}
Ok my question is; In my insert method, which adds a new element into the array I'm having problems accessing the elements data from within the array.
template <typename K, typename DT>
void myMap<K, DT>::insert(const K &key, const DT &val)
{
/*A temporary element to store the data which is then stored
in the array.*/
myElement *temp = new myElement;
temp->key = key;
temp->data = val;
elements[i]->key //This isn't allowed, It's not letting me access the key for the element at I
delete temp;
}
I can get to it like this:
elements->key;
Basically I need to be able to access the array within the class and access the data from the element class so that I can check if the key is already present to avoid duplication.
Upvotes: 0
Views: 1308
Reputation: 10064
Use elements[i].key
The elements array is a pointer, but when you array index it, you just get a regular old myElement object.
Usually the compiler's error message is pretty clear on this. I'd go back and reread the error you got.
Upvotes: 2