模板化的类和友元函数
Templated class and friend functions
我有一个非常简单的代码,我正在尝试创建类模板。不幸的是,我为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();
}
相关文章:
- C++模板来检查友元函数的存在
- 如何使用单独文件中的派生类访问友元函数对象
- 模板化的类和友元函数
- 友元函数无法访问私有数据成员 (c++)
- 继承和友元函数,从基类访问受保护的成员
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- C++ 友元函数和私有构造函数
- 使第二个类的构造函数成为第一个类中的友元函数
- 未定义的类模板不会实例化以检查友元函数
- C++类中的友元函数有问题?
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 模板类中的模板友元函数
- C++ 17 个友元函数声明和内联命名空间
- 友元函数需要一个帮助程序函数
- 在类内定义的友元函数与类外定义的友元函数的查找规则之间的差异
- 运算符重载在 C++ 中使用友元函数
- 类C++友元函数无法访问封装的类
- 函数指针作为友元函数