C++ 友元函数的模板编译
我试着从我的例子。但是在数据声明上不允许接收诸如“~”:“friend”之类的编译错误。有人能提供更多细节吗?小心。这段代码有很多问题。这里是使用C++ 4.3:编译的精良版本C++ 友元函数的模板编译,c++,templates,C++,Templates,我试着从我的例子。但是在数据声明上不允许接收诸如“~”:“friend”之类的编译错误。有人能提供更多细节吗?小心。这段代码有很多问题。这里是使用C++ 4.3:编译的精良版本 #include<iostream> #include<vector> using std::cin; using std::cout; using std::vector; using std::endl; const int ROWS = 2; const int COLS = 2; te
#include<iostream>
#include<vector>
using std::cin;
using std::cout;
using std::vector;
using std::endl;
const int ROWS = 2;
const int COLS = 2;
template<class T>
class matrix
{
public:
//declare a vector of vectors of type T
vector< vector<T> > s ;
//Initialize the size of s to ROWS by COLS
matrix(): s(ROWS, vector<T>(COLS)) {}
void readm();
void printm();
//declare the operators +,-,*,~ as friends and with return type matrix<T>
template< typename T1>
friend matrix<T1> operator+(const matrix&, const matrix&);
template< typename T1>
friend matrix<T1> operator-(const matrix&, const matrix&);
template< typename T1>
friend matrix<T1> operator*(const matrix<T>&, const matrix<T>&);
template< typename T1>
friend matrix<T1> operator~(const matrix<T>&);
};
template<class T>
void matrix<T>::readm()
{
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
cin >> this->s[i][j];
}
template<class T>
void matrix<T>::printm()
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
cout<< this->s[i][j] <<"\t";
cout << endl;
}
}
template<class T>
matrix<T> operator+(const matrix<T>& a, const matrix<T>& b)
{
//declare a matrix temp of type T to store the result and return this matrix
matrix<T> temp;
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
temp.s[i][j] = a.s[i][j] + b.s[i][j];
return temp;
}
template<class T>
matrix<T> operator-(const matrix<T>& a, const matrix<T>& b)
{
matrix<T> temp;
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
temp.s[i][j] = a.s[i][j] - b.s[i][j];
return temp;
}
template<class T>
matrix<T> operator*(const matrix<T>& a, const matrix<T>& b)
{
matrix<T> temp;
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
temp.s[i][j] = 0;
for(int k = 0; k < COLS; k++)
temp.s[i][j] += a.s[i][k] * b.s[k][j];
}
}
return temp;
}
template<class T>
matrix<T> operator~(const matrix<T>& trans)
{
matrix<T> temp;
for(int i = 0; i < ROWS; i++)
for(int j = 0; j < COLS; j++)
temp.s[j][i] = trans.s[i][j];
return temp;
}
int main()
{
matrix<int> a,b,c;
//we can also declare matrices of type int,float,double etc.
cout<<"Enter matrix a:"<<endl;
a.readm();
cout<<"a is:"<<endl;
a.printm();
cout<<"Enter matrix b:"<<endl;
b.readm();
cout<<"b is:"<<endl;
b.printm();
c = a + b;
cout<<endl<<"Result of a+b:"<<endl;
c.printm();
c = a - b;
cout<<endl<<"Result of a-b:"<<endl;
c.printm();
c = a*b;
cout << endl << "Result of a*b:";
c.printm();
cout << "Result of a+b*c is:";
(a+b*c).printm();
c = ~(a+b*c);
cout << "Result of transpose of a+b*c is:";
c.printm();
}
#包括
#包括
使用std::cin;
使用std::cout;
使用std::vector;
使用std::endl;
const int ROWS=2;
常数int COLS=2;
模板
类矩阵
{
公众:
//声明一个T型向量的向量
向量s;
//通过COLS将s的大小初始化为行
矩阵():s(行,向量(COLS)){}
void readm();
void printm();
//将运算符+、-、*、~声明为友元,并使用返回类型矩阵
模板
友元矩阵算子+(常数矩阵&,常数矩阵&);
模板
友元矩阵算子-(常数矩阵&,常数矩阵&);
模板
友元矩阵算子*(常数矩阵&,常数矩阵&);
模板
友元矩阵算子(常数矩阵&);
};
模板
void矩阵::readm()
{
对于(int i=0;i>本->s[i][j];
}
模板
void矩阵::printm()
{
对于(int i=0;i 可以给我们一个线索:消息指的是哪一行吗?奥利,错误与使用友元矩阵运算符*(常数矩阵&,常数矩阵&)和友元矩阵运算符(常数矩阵&)有关;非常感谢。它现在可以正常工作。一旦我为每个friend函数添加了模板,它现在就可以正常工作了。但您能解释一下它的含义吗?这是一个模板friend函数。请参见以下示例: