C++ 使用「;这";模板类中的关键字
我定义了一个模板类,并通过以下方式重载了一个运算符:C++ 使用「;这";模板类中的关键字,c++,class,templates,compiler-errors,C++,Class,Templates,Compiler Errors,我定义了一个模板类,并通过以下方式重载了一个运算符: template<class T = bool> class SparseMatrix { public: SparseMatrix(int msize); //Multiplication by vectors or matrices SparseMatrix<double> operator *(SparseMatrix<T> &s); //Matrix expo
template<class T = bool>
class SparseMatrix
{
public:
SparseMatrix(int msize);
//Multiplication by vectors or matrices
SparseMatrix<double> operator *(SparseMatrix<T> &s);
//Matrix exponentiation
SparseMatrix<double> pow(int n);
};
template<class T>
SparseMatrix<double> SparseMatrix<T>::pow(int n)
{
if (n == 2)
{
return (this * this); //ERROR
}
else
{
int i=0;
SparseMatrix<double> s = this * this;
while (i < n-2)
{
s = s * this;
i++;
}
return s;
}
}
然而,当我转到main
并编写类似SparseMatrix u=t.pow(2)的内容时
我得到一个错误,指出类型为'SparseMatrix*'和'SparseMatrix*'的操作数对二进制'operator*'无效。
。乘法对于bool
矩阵定义得很好,正如我前面所说的,那么,为什么编译器会抱怨呢?我是否使用了这个?如何修复此错误
谢谢你的帮助 此
是指向对象的指针,而不是对象本身。解除触发此
应该可以完成此操作。此
是指向对象的指针,而不是对象本身。解除触发此
应该可以达到目的。您似乎忘记了此
是指向对象的指针。如果您实际阅读了错误消息(其中提到类型SparseMatrix*
),则应该非常明显。顺便说一句,所有const
都丢失了。如果您实现operator*=()
以及operator*()
。您似乎忘记了这是指向对象的指针。如果您实际阅读了错误消息(其中提到类型SparseMatrix*
),则应该非常明显。顺便说一句,所有const
都丢失了。如果您实现operator*=()
以及operator*
,您会发现它更容易、更有效。
template<class T>
SparseMatrix<double> SparseMatrix<T>::pow(int n)
{
if (n == 2)
{
return (this * this); //ERROR
}
else
{
int i=0;
SparseMatrix<double> s = this * this;
while (i < n-2)
{
s = s * this;
i++;
}
return s;
}
}