c++类模板问题

2024-12-01 12:54:45
推荐回答(3个)
回答1:

#include
#include

using namespace std;
template < class T > class Matrix {
public:
Matrix(int);
~Matrix();
void setMatrix(T **);
Matrix operator=(const Matrix < T > &);

Matrix operator +(const Matrix < T > &);
Matrix operator -(const Matrix < T > &);
Matrix operator *(const Matrix < T > &);

void output();

private:
T ** matrix;
int rows;
int cols;
};
template < class T > Matrix < T >::Matrix(int size)
{
rows = cols = size;
matrix = new T *[rows];
for (int i = 0; i < rows; i++)
matrix[i] = new T[cols];
for (int i1 = 0; i1 < rows; i1++) {
for (int j1 = 0; j1 < cols; j1++)
matrix[i1][j1] = 0;
}

}

template < class T > Matrix < T >::~Matrix()
{
for (int i = 0; i < rows; i++)
delete[]matrix[i];
delete[]matrix;
}

template < class T > void Matrix < T >::setMatrix(T ** data)
{

for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = data[i][j];
}

template < class T >
Matrix < T > Matrix < T >::operator =(const Matrix < T > &b)
{
for (int i = 0; i < b.rows; i++) {
for (int j = 0; j < b.cols; j++)
matrix[i][j] = b.matrix[i][j];
}
return *this;
}

template < class T >
Matrix < T > Matrix < T >::operator +(const Matrix < T > &b)
{
Matrix < T > a(this->rows);

int i, j;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
a.matrix[i][j] = matrix[i][j] + b.matrix[i][j];
return a;
}

template < class T >
Matrix < T > Matrix < T >::operator -(const Matrix < T > &b)
{
Matrix < T > a(b.rows);
int i, j;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
a.matrix[i][j] = matrix[i][j] - b.matrix[i][j];
return a;
}

template < class T >
Matrix < T > Matrix < T >::operator *(const Matrix < T > &b)
{
Matrix < T > a(b.rows);
int i, j, k;
for (i = 0; i < rows; i++)
for (k = 0; k < rows; k++)
for (j = 0; j < cols; j++)
a.matrix[i][k] +=
matrix[i][j] * b.matrix[j][i];
return a;
}
template < class T > void Matrix < T >::output()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
}

int main()
{
Matrix < int >a(3), b(3);
// 我想实现上述重载运算符的功能,怎么写?

(a - b).output();
(a + b).output();
(a * b).output();

return 0;
}

你的错误和类模板无关。

回答2:

能成功运行了

代码如下

#include
#include

using namespace std;
template
class Matrix{
public:
Matrix(){};
Matrix(int);
~Matrix();
void setMatrix(T**);
Matrix operator=(const Matrix &);

Matrix operator +(const Matrix&);
Matrix operator -(const Matrix&);
Matrix operator *(const Matrix&);

void output();

public:
T **matrix;
int rows;
int cols;
};
template
Matrix::Matrix(int size)
{
rows=cols=size;
matrix=new T *[rows];
for(int i=0;i {
matrix[i]=new T[cols];
}

for(int i1=0;i1 {
for(int j1=0;j1 {
matrix[i1][j1]=0;
}
}

}
template
Matrix::~Matrix()
{
for(int i=0;i delete [] matrix[i];

}

template
void Matrix::setMatrix(T **data)
{

for(int i=0;i for(int j=0;j matrix[i][j]=data[i][j];
}

template
Matrix Matrix::operator =(const Matrix &b)
{
for(int i=0;i {
for(int j=0;j matrix[i][j]=b.matrix[i][j];
}
return *this;
}

template
Matrix Matrix::operator +(const Matrix &b)
{
Matrix a(*this);
int i,j;
for(i=0;i for(j=0;j a.matrix[i][j]=matrix[i][j]+b.matrix[i][j];
return a;
}

template
Matrix Matrix::operator -(const Matrix &b)
{
Matrix a;
int i,j;
for(i=0;i for(j=0;j a.matrix[i][j]=matrix[i][j]-b.matrix[i][j];
return a;
}
template
Matrix Matrix::operator *(const Matrix &b)
{
Matrix a(b.rows);
int i,j,k;
for(i=0;i for(k=0;k for(j=0;j a.matrix[i][k]+=matrix[i][j]*b.matrix[j][i];
return a;
}
template
void Matrix::output()
{
for(int i=0;i {
for(int j=0;j {
cout< }
cout< }
}

int main()
{
Matrix a(3);
a.output();

return 0;
}

回答3:

类模板是用简单的替换法则的,你的cpp由于没有相应的实现实例无法生成对应的.obj对象
解决方案是用export关键字定义模板,但是现在基本没有一个C++编译器实现了这个关键字(gcc4.5和VS2010都没)
于是现在你只能把那个cpp文件全部copy到头文件里去

详见C++ Primer中的说明

以下是export的简单介绍

PS.别在头文件中用这句
using namespace std;
这是很不好的习惯