朋友功能的模板编译

时间:2011-08-11 11:38:27

标签: c++ templates

我正在尝试http://www.acims.arizona.edu/PUBLICATIONS/PDF/RajnikanthTemplates04.pdf的例子。但接收编译错误,如'〜':'朋友'不允许数据声明。任何人都可以提供更多细节吗?

1 个答案:

答案 0 :(得分:2)

小心。那段代码有很多问题。这是使用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;

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