C++新手类模板函数定义问题

2025-04-30 02:22:18
推荐回答(1个)
回答1:

你这叫类的成员函数mat::operator*(const double, mat&),而类的声明中并没有这样一个函数,所以报错。

把这个函数改成友元函数形式:

class mat
{
private:
    ...
public:
    ...
    template
    friend mat operator*(const double a, mat& A);
};
template 
mat operator*(const double a, mat& A){
    return A*a;
}