gpt4 book ai didi

c++ - 无法将函数定义与 cpp 中的现有声明相匹配

转载 作者:行者123 更新时间:2023-11-28 06:28:56 25 4
gpt4 key购买 nike

我正在做一个类(class)项目,我在 cpp 中创建了一个带有二维 vector 的矩阵类。我正在尝试使用矩阵 obj 将 * 运算符覆盖为全局运算符。

这是我的声明:

friend Matrix<T> operator * (T t, const Matrix<T> &m);

这是函数:

template <typename T>
Matrix<T> operator * (T t, const Matrix<T> &m)
{
int i, j;
Matrix<T> ans(rows, cols);
for (i = 0; i < rows; i++)
{
for (j = 0; j < rows; j++)
{
ans[i][j] = t * m.mat[i][j];
}
}
return ans;
}

我的错误是:错误 C2244:'Matrix::operator *':无法将函数定义与现有声明相匹配

我的代码有什么问题??

最佳答案

您声明的 friend 函数虽然在类中,但不是成员函数。
像这样调整定义:

template <typename T>
Matrix<T> operator * (T t, const Matrix<T> &m)
{
// […]
}

关于c++ - 无法将函数定义与 cpp 中的现有声明相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28001441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com