Template Class Assignment Operator Overloading

128 views Asked by At

I'm having a little trouble with the following:

I'm writing a map abstract data type for some coursework & I've come across a problem whilst trying to assign an object of my class (MapEntry - below) to an array of the same type in the class MapADT. It tells me that:

Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'MapEntry *' (or there is no acceptable conversion) c:\users\cross_000\documents\visual studio 2013\projects\objectorientedmethodsasignment1\mapadt\mapadt.h 14

So I thought I would write my own Assignment operator override. I've done this in the MapEntry class definition but the compiler doesn't seem to recognize it when I try to initialize the array in the constructor on MapADT - below.

Any help would be greatly appreciated.

#pragma once

template <class Tk, class Tc>
class MapEntry
{
private:
    Tk key;
    Tc contents;
    bool isPopulated;

public:
    MapEntry() {

    }

    MapEntry(Tk keyInput, Tc contentsInput) {
        key = keyInput;
        contents = contentsInput;
        isPopulated = true;
    }

    MapEntry(Tk keyInput, Tc contentsInput, bool isPopulatedInput) {
        key = keyInput;
        contents = contentsInput;
        isPopulated = isPopulatedInput;
    }

    ~MapEntry() {
        //TODO
    }

    Tk getKey() {
        return key;
    }

    void setKey(Tk keyInput) {
        key = keyInput;
    }

    Tc getContents() {
        return contents;
    }

    void setContents(Tc contentsInput) {
        contents = contentsInput;
    }

    bool getIsPopulated() {
        return isPopulated;
    }

    void setIsPopulated(bool isPopulatedInput) {
        isPopulated = isPopulatedInput;
    }

    MapEntry<Tk, Tc>& operator=(const MapEntry<Tk, Tc> & lst)
    {
        clear();
        copy(lst);
        return *this;
    }
};

MapADT.h

#pragma once
#include "MapEntry.h"

template <class Tk, class Tc>
class MapADT
{
private:
    int mapSize = 1000;
    MapEntry<Tk, Tc> *map;
public:
    MapADT() {
        map = new MapEntry<Tk, Tc>[mapSize];
        for (int i = 0; i < mapSize; i++) {
            map[i] = new MapEntry<Tk, Tc>(NULL, NULL, false);
        }
    }
}

There's more to the MapADT class but I don't think it's relevant. If you need to see the whole thing I can add it.

2

There are 2 answers

1
Jarod42 On BEST ANSWER

map[i] is not an pointer to MapEntry.

Not sure if you want

map[i] = MapEntry<Tk, Tc>(NULL, NULL, false);

or

MapEntry<Tk, Tc>** map;

MapADT() {
    map = new MapEntry<Tk, Tc>*[mapSize];
    for (int i = 0; i < mapSize; i++) {
        map[i] = new MapEntry<Tk, Tc>(NULL, NULL, false);
    }
}

to solve your issue.

1
Anton Savin On

In this line

map = new MapEntry<Tk, Tc>[mapSize];

you allocate an array of MapEntry<Tk, Tc>, and default constructors are called for all of them. There's no need in subsequent for loop at all, you should just write proper initialization in MapEntry::MapEntry(), which is currently empty.