智联招聘网站怎么做微招聘信息,中铁建设中南公司官方网站,wordpress调整配置文件怎么写,六安做网站的作者#xff1a;非妃是公主 专栏#xff1a;《C》 博客地址#xff1a;https://blog.csdn.net/myf_666 个性签#xff1a;顺境不惰#xff0c;逆境不馁#xff0c;以心制境#xff0c;万事可成。——曾国藩 文章目录专栏推荐一、类的声明及函数定义二、错误信息三、问题… 作者非妃是公主 专栏《C》 博客地址https://blog.csdn.net/myf_666 个性签顺境不惰逆境不馁以心制境万事可成。——曾国藩 文章目录专栏推荐一、类的声明及函数定义二、错误信息三、问题解决1. 解决过程2. 全部代码四、总结the end…… 专栏推荐
专栏名称专栏地址软件工程专栏——软件工程计算机图形学 专栏——计算机图形学 操作系统专栏——操作系统软件测试专栏——软件测试机器学习专栏——机器学习数据库专栏——数据库算法专栏——算法
一、类的声明及函数定义
类的声明及 *运算符重载 函数声明如下 定义如下 值得注意的是上面的 和 - 两个运算符的重载并没有问题。存在问题的是 * 运算符的重载看似和上面一样但是却报出了如下错误。 二、错误信息
已启动生成...
1------ 已启动生成: 项目: P2022_10, 配置: Debug x64 ------
122_矩阵.cpp
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,28): error C2143: 语法错误: 缺少“;”(在“”的前面)
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(36): message : 查看对正在编译的 类 模板 实例化“MatrixT”的引用
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,19): error C2460: “*”: 使用正在定义的“MatrixT”
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2433: “*”: 不允许在数据声明中使用“friend”
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2473: “operator *”: 看起来像函数定义但却没有参数列表。
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2238: 意外的标记位于“;”之前
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(124,11): error C2365: “*”: 重定义以前的定义是“数据变量”
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35): message : 参见“*”的声明
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(35,19): error C2460: “*”: 使用正在定义的“Matrixint”
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(150): message : 参见“Matrixint”的声明
1D:\CCode\leCoPractice\P2022_10\22_矩阵.cpp(193,19): error C3861: “multi”: 找不到标识符
1已完成生成项目“P2022_10.vcxproj”的操作 - 失败。“生成”: 0 成功1 失败0 更新0 已跳过 如果你也报出了相同的错误那么可以继续往后看下去。 三、问题解决
1. 解决过程
因为这个问题涉及到了模板而自己平时对于模板的编写并不熟练。
首先需要说明一点的是C 的运算符重载一般有两种方式
采用友元的方式比如重载输入输出运算符。另一种方式是采用成员函数的形式。
具体的定义方式可以去网上查看一些实例代码在此不再赘述。
在检索了网络上的一些矩阵模板类我最终的解决方法是把友元函数的重载方式改为成员函数类型的成功解决了这个问题。
主要变动如下 可以看到不同于上面 和 - 重载中友元定义方式了。
函数定义如下 2. 全部代码
修改完之后重新运行发现解决了问题输出结果如下 全部源码如下
#includeiostream
using namespace std;template class T
class Matrix
{typedef MatrixT Myt;
protected:T* m_pDatats; //数组int m_stRow, m_stCol; //行数和列数public://构造函数Matrix(int stRow, int stCol);//复制构造函数Matrix(const Myt rhs);//析构函数~Matrix();//矩阵初始化void Initialize(const T* rhs, int stRow, int stCol);// 取值函数T getValue(int row, int col);// 设置值函数void setValue(int row, int col, T value);// 矩阵运算符相加friend MatrixT operatorT(const MatrixT lhs, const MatrixT rhs);// 矩阵运算相减friend MatrixT operator-T(const MatrixT lhs, const MatrixT rhs);// 矩阵运算相乘MatrixT operator* (MatrixT rhs);
};//实现构造函数
templateclass T
MatrixT::Matrix(int stRow, int stCol)
{m_stRow stRow;m_stCol stCol;m_pDatats new T[stRow * stCol];
}// 实现复制构造函数
templateclass T
MatrixT::Matrix(const Myt rhs)
{m_pDatats new T[rhs.m_stRow * rhs.m_stCol];m_stRow rhs.m_stRow;m_stCol rhs.m_stCol;Initialize(rhs.m_pDatats, rhs.m_stRow, rhs.m_stCol);
}//矩阵初始化的实现
templateclass T
void MatrixT::Initialize(const T* rhs, int stRow, int stCol)
{//用一维数组表示二位数组for (int i 0; i stRow * stCol; i){m_pDatats[i] rhs[i];}
}//实现析构函数
templateclass T
MatrixT::~Matrix() {if (m_pDatats ! nullptr) {delete[] m_pDatats;m_pDatats nullptr;}
}// 获取矩阵值
templateclass T
T MatrixT::getValue(int row, int col) {return m_pDatats[row * m_stRow col];
}//设置值函数
templateclass T
void MatrixT::setValue(int row, int col, T value) {m_pDatats[row * m_stRow col] value;
}//矩阵相加的实现
templateclass T
MatrixT operator(const MatrixT lhs, const MatrixT rhs)
{if (lhs.m_stCol ! rhs.m_stCol || lhs.m_stRow ! rhs.m_stRow) {MatrixT tmp(0, 0);return tmp;}else {MatrixT tmp(rhs.m_stRow, rhs.m_stCol);for (int i 0; i rhs.m_stRow * rhs.m_stCol; i) {tmp.m_pDatats[i] lhs.m_pDatats[i] rhs.m_pDatats[i];}return tmp;}
}
//矩阵相减的实现
templateclass T
MatrixT operator-(const MatrixT lhs, const MatrixT rhs)
{if (lhs.m_stCol ! rhs.m_stCol || lhs.m_stRow ! rhs.m_stRow) {MatrixT tmp(0, 0);return tmp;}else {MatrixT tmp(rhs.m_stRow, rhs.m_stCol);for (int i 0; i rhs.m_stRow * rhs.m_stCol; i) {tmp.m_pDatats[i] lhs.m_pDatats[i] - rhs.m_pDatats[i];}return tmp;}
}
//矩阵运算相乘的实现
templateclass T
MatrixT MatrixT::operator* (MatrixT rhs)
{if (m_stRow ! rhs.m_stCol || m_stCol ! rhs.m_stRow) {MatrixT tmp(0, 0);return tmp;}else {MatrixT tmp(m_stRow, rhs.m_stCol);for (int i 0; i tmp.m_stRow; i) {for (int j 0; j tmp.m_stCol; j) {int value 0;for (int k 0; k m_stCol; k) {value getValue(i, k) * rhs.getValue(k, j);}tmp.setValue(i, j, value);}}return tmp;}
}
//主函数int main()
{int row 3;int col 3;Matrixint m1(row, col);int rhs[9] { 1,2,3,4,5,6,7,8,9 };m1.Initialize(rhs, row, col);//输出矩阵cout 输出的矩阵m1 endl;for (int i 0; i row; i){for (int j 0; j col; j) {cout m1.getValue(i, j) ;}cout endl;}Matrixint m2(row, col);m2.Initialize(rhs, row, col);//输出矩阵cout 输出的矩阵m2 endl;for (int i 0; i row; i){for (int j 0; j col; j) {cout m2.getValue(i, j) ;}cout endl;}//两矩阵相加Matrixint res m1 m2;cout 两矩阵输出的结果矩阵为 endl;for (int i 0; i row; i) {for (int j 0; j col; j) {cout res.getValue(i, j) ;}cout endl;}//两矩阵相减Matrixint cut m1 - m2;cout 两矩阵相减输出的结果矩阵为 endl;for (int i 0; i row; i) {for (int j 0; j col; j) {cout cut.getValue(i, j) ;}cout endl;}// 两矩阵相乘Matrixint xc m1 * m2;cout 两矩阵相减输出的结果矩阵为 endl;for (int i 0; i row; i) {for (int j 0; j col; j) {cout xc.getValue(i, j) ;}cout endl;}return 0;
}四、总结
虽然解决了问题但由于经历有限而且其实是帮别人调的代码自己很少去写模板的 。这个小bug我并没有详细的去追踪他产生的根源。
具体产生的原因也给出了两个推测
在C中* 运算符是不能通过友元函数的方式进行运算符重载的。如果重载只能采用成员函数的方式。因为除了输入、输出之外。友元函数在C中其实并不提倡因为他破坏了类的封装性。所以我推测产生的原因可能是随着 C 标准的不断迭代逐渐在边缘化友元函数。
以上就是我的两点推测但没有去详细探究如有纰漏欢迎各位在评论区或者私信进行指正感谢 the end……
关于C * 运算符重载的一个小 bug 到这里就要结束啦~~到此既是缘分欢迎您的点赞、评论、收藏关注我不迷路我们下期再见 我是Cherries一位计算机科班在校大学生写博客用来记录自己平时的所思所想 内容繁杂又才疏学浅难免存在错误欢迎各位大佬的批评指正 我们相互交流共同进步 注本文由 非妃是公主 发布于https://blog.csdn.net/myf_666转载请务必标明原文链接https://blog.csdn.net/myf_666/article/details/129264092