Need help for my template class example

/*In the following code I made 'myArray' as class template. So how can I handle the friend function (operator *) in this scenario?

compilation error:error: invalid use of template-name 'myArray' without an argument list myArray operator * (myArray &a1, myArray &a2)*/

#include<iostream>
using namespace std;

const int sz = 3;

template <typename T>
class myArray
{
T *arr;
const static int size = 3;

public:
myArray()
{
arr = new T[size];
for (int i=0; i<size; i++)
arr[i] = 0;
}

myArray(T *actArray)
{
arr = new T[size];
for (int i=0; i<size; i++)
arr[i] = actArray[i];
}

void prArray()
{
cout<<endl<<endl;
for (int i=0; i<size; i++)
cout << "arr [" << i << "] = " << arr[i] << endl;
}

friend myArray operator * (myArray &arr1, myArray &arr2);
};


myArray operator * (myArray &a1, myArray &a2)
{
myArray product;
for (int i=0; i<sz; i++)
product.arr[i] = a1.arr[i] * a2.arr[i];

return product;
}

int main()
{
int xi[3] = {1, 2, 3};
int yi[3] = {5, 5, 5};

float xf[3] = {1.1, 2.1, 3.1};
float yf[3] = {5.5, 5.5, 5.5};

//considering template class as integer
myArray <int>a1;
myArray <int>a2;
a1 = xi;
a2 = yi;

a1.prArray();
a2.prArray();
cout<<"Interger class..."<<endl;
myArray <int>a3;
a3 = a1 * a2;
a3.prArray();

/*//considering template class as float
myArray <float>b1, <float>b2;
b1 = xi;
b2 = yi;

b1.prArray();
b2.prArray();
cout<<"Float class..."<<endl;
myArray <float>b3;
b3 = b1 * b2;
b3.prArray();*/
}


Last edited on
friend operators of tempates can be a little bit tricky,
check out cppreference: https://en.cppreference.com/w/cpp/language/friend#Template_friend_operators
or the c++ faq https://isocpp.org/wiki/faq/templates#template-friends
Thankyou very much Cubbi,

The second link is very much useful for me.
Topic archived. No new replies allowed.