
作者:非妃是公主
专栏:《C++》
博客地址:https://blog.csdn.net/myf_666
个性签:顺境不惰,逆境不馁,以心制境,万事可成。——曾国藩
文章目录
- 专栏推荐
- 一、类的声明及函数定义
- 二、错误信息
- 三、问题解决
- 1. 解决过程
- 2. 全部代码
- 四、总结
- the end……
专栏推荐
| 专栏名称 | 专栏地址 | 
|---|---|
| 软件工程 | 专栏——软件工程 | 
| 计算机图形学 | 专栏——计算机图形学 | 
| 操作系统 | 专栏——操作系统 | 
| 软件测试 | 专栏——软件测试 | 
| 机器学习 | 专栏——机器学习 | 
| 数据库 | 专栏——数据库 | 
| 算法 | 专栏——算法 | 
一、类的声明及函数定义
类的声明及 *运算符重载 函数声明如下:

定义如下:

值得注意的是,上面的 + 和 - 两个运算符的重载并没有问题。存在问题的是 * 运算符的重载,看似和上面一样,但是却报出了如下错误。
二、错误信息
已启动生成...
1>------ 已启动生成: 项目: P2022_10, 配置: Debug x64 ------
1>22_矩阵.cpp
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,28): error C2143: 语法错误: 缺少“;”(在“<”的前面)
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(36): message : 查看对正在编译的 类 模板 实例化“Matrix<T>”的引用
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,19): error C2460: “*”: 使用正在定义的“Matrix<T>”
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2433: “*”: 不允许在数据声明中使用“friend”
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2473: “operator *”: 看起来像函数定义,但却没有参数列表。
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,1): error C2238: 意外的标记位于“;”之前
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(124,11): error C2365: “*”: 重定义;以前的定义是“数据变量”
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35): message : 参见“*”的声明
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(35,19): error C2460: “*”: 使用正在定义的“Matrix<int>”
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(150): message : 参见“Matrix<int>”的声明
1>D:\C++Code\leCoPractice\P2022_10\22_矩阵.cpp(193,19): error C3861: “multi”: 找不到标识符
1>已完成生成项目“P2022_10.vcxproj”的操作 - 失败。
========== “生成”: 0 成功,1 失败,0 更新,0 已跳过 ==========
如果你也报出了相同的错误,那么可以继续往后看下去。
三、问题解决
1. 解决过程
因为这个问题涉及到了模板,而自己平时对于模板的编写并不熟练。
首先,需要说明一点的是,C++ 的运算符重载一般有两种方式,
- 采用友元的方式,比如重载输入输出运算符。
- 另一种方式是采用成员函数的形式。
具体的定义方式,可以去网上查看一些实例代码,在此不再赘述。
在检索了网络上的一些矩阵模板类,我最终的解决方法是:把友元函数的重载方式改为成员函数类型的,成功解决了这个问题。
主要变动如下:

可以看到,不同于上面( + 和 - )重载中友元定义方式了。
函数定义如下:

2. 全部代码
修改完之后,重新运行,发现解决了问题,输出结果如下:

全部源码如下:
#include<iostream>
using  namespace std;
template <class T>
class Matrix
{
	typedef  Matrix<T> 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 Matrix<T> operator+<T>(const Matrix<T>& lhs, const Matrix<T>& rhs);
	// 矩阵运算相减
	friend Matrix<T> operator-<T>(const Matrix<T>& lhs, const Matrix<T>& rhs);
	// 矩阵运算相乘
	Matrix<T> operator* (Matrix<T>& rhs);
};
//实现构造函数
template<class T>
Matrix<T>::Matrix(int stRow, int stCol)
{
	m_stRow = stRow;
	m_stCol = stCol;
	m_pDatats = new T[stRow * stCol];
}
// 实现复制构造函数
template<class T>
Matrix<T>::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);
}
//矩阵初始化的实现
template<class T>
void Matrix<T>::Initialize(const T* rhs, int stRow, int stCol)
{
	//用一维数组表示二位数组
	for (int i = 0; i < stRow * stCol; i++)
	{
		m_pDatats[i] = rhs[i];
	}
}
//实现析构函数
template<class T>
Matrix<T>::~Matrix() {
	if (m_pDatats != nullptr) {
		delete[] m_pDatats;
		m_pDatats = nullptr;
	}
}
// 获取矩阵值
template<class T>
T Matrix<T>::getValue(int row, int col) {
	return m_pDatats[row * m_stRow + col];
}
//设置值函数
template<class T>
void Matrix<T>::setValue(int row, int col, T value) {
	m_pDatats[row * m_stRow + col] = value;
}
//矩阵相加的实现
template<class T>
Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs)
{
	if (lhs.m_stCol != rhs.m_stCol || lhs.m_stRow != rhs.m_stRow) {
		Matrix<T> tmp(0, 0);
		return tmp;
	}
	else {
		Matrix<T> 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;
	}
}
//矩阵相减的实现
template<class T>
Matrix<T> operator-(const Matrix<T>& lhs, const Matrix<T>& rhs)
{
	if (lhs.m_stCol != rhs.m_stCol || lhs.m_stRow != rhs.m_stRow) {
		Matrix<T> tmp(0, 0);
		return tmp;
	}
	else {
		Matrix<T> 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;
	}
}
//矩阵运算相乘的实现
template<class T>
Matrix<T> Matrix<T>::operator* (Matrix<T>& rhs)
{
	if (m_stRow != rhs.m_stCol || m_stCol != rhs.m_stRow) {
		Matrix<T> tmp(0, 0);
		return tmp;
	}
	else {
		Matrix<T> 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;
	Matrix<int>  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;
	}
	Matrix<int>  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;
	}
	//两矩阵相加
	Matrix<int> 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;
	}
	//两矩阵相减
	Matrix<int> 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;
	}
	// 两矩阵相乘
	Matrix<int> 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




















