AlexGimson
AlexGimson

Reputation: 19

Compiler error when dynamically allocating a template class where type is another object of template type

This is the compiler error that I am currently getting

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Pair' (or there is no acceptable conversion)

using namespace std;

template<typename T, typename U>
Array< Pair<T, U>>* zip(Array<T> & lhs,Array<U> & rhs) 
{
    int zipLen = (lhs.getLength() < rhs.getLength() ? lhs.getLength() : rhs.getLength());

    Array<Pair<T, U>>* zipped= new Array<Pair<T,U>>(zipLen);

    for (int i=0; i<zipLen; i++)
        zipped[i] = Pair<T, U>(lhs[i], rhs[i]);//and this is the line giving me problems

    return zipped;
}

int main()
{
    Array<int> a1(5);
    Array<char>a2(3);

    Array<Pair<int,char>>*a3;

    for(int i =1;i<5;i++)
        a1[i-1]=i;

    for(char ch='a';ch<='c';ch++)
        a2[ch-'a']=ch;

    a3=zip(a1,a2);

    cout<<a3;

    system("pause");
    return 0;
}

Upvotes: 1

Views: 354

Answers (1)

Sam Manzer
Sam Manzer

Reputation: 1220

I believe that you have declared "zipped" as a pointer to an Array of pairs. Therefore, to access elements of the array, you must first dereference the pointer:

(*zipped)[i] = Pair<T, U>(lhs[i], rhs[i]);

Upvotes: 2

Related Questions