模板化的类和朋友函数

时间:2018-11-01 19:12:30

标签: c++ arrays templates

我有一个非常简单的代码,试图创建类模板。不幸的是,我的朋友功能代码出错。

在以下代码“ myArray”中作为模板类。在这种情况下,如何处理朋友功能operator *(...)?使用我当前的代码,我得到以下错误:

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

2 个答案:

答案 0 :(得分:0)

operator*的正文移到类的正文中:

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; 
}

也:

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; 
}

通过有效的移动,可以减少创建的临时对象。

答案 1 :(得分:0)

//最后,我在类本​​身中定义了朋友函数以及Peter的建议。...所以最终的代码是...感谢大家的帮助

但是仍然不明白为什么我们将其作为const参数...?

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