模板化的类和友元函数

Templated class and friend functions

本文关键字:友元 函数      更新时间:2023-10-16

我有一个非常简单的代码,我正在尝试创建类模板。不幸的是,我为friend函数编写的代码出现了错误。

在以下代码中,"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();*/
}

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

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

//最后,我在类本身中定义了friend函数,并给出了Peter…的建议。。。。所以最后的代码是…谢谢大家的帮助

但仍然不明白为什么我们把它作为常量参数。。。?

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