简体   繁体   中英

Templated class and friend functions

I have a very simple code where I am trying to create class template. Unfortunately, I am getting error with my code for the friend function.

In the following code 'myArray' as a template class. How can I handle the friend function operator *(...) in this scenario? With my current code, I get following error:

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

Here is the code:

#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();*/
}

Move the body of operator* into the body of the class:

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

  return product; 
}

Also:

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

  return *this;
}
friend myArray operator * (myArray arr1, myArray const& arr2) {
  arr1*=arr2;
  return arr1; 
}

with efficient move this reduces temporary objects created.

//Finally I define the friend function in the class itself along with the suggestions made by Peter.... so the final code is...Thanks all for your help

But still didn't understand why we made it as const parameters...?

 #include<iostream>
 using namespace std;

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<T> operator* (const myArray<T>& a1, const myArray<T>& a2)
    {
        myArray <T> product;
        for (int i=0; i<size; 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;
myArray <float>b2; 
b1 = xf;
b2 = yf;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM