自定义矩阵模板类如何重载乘法?

_iChthyosaur 2017-11-22 10:38:05
自定义了一个矩阵模板类,做数乘运算时如果数在前对象在后就要用友元重载一次乘法,但是怎么写都不对。最好的情况是代码能通过编译但是无法调用,一调用链接器就报无法解析的外部命令。最差的情况就是一堆错误,编译都不给通过。
//这是提前声明
template <typename T> class MyMatrix;
template <typename T>
MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);
//类中声明
friend MyMatrix operator*(const T k, const MyMatrix & ma);
//友元函数定义
template <typename T>
MyNamespace::MyMatrix<T> MyNamespace::operator*(const T k, const MyMatrix<T> & ma)
{
MyMatrix<T> result(ma.row, ma.col);
for (int i = 0; i < result.row; i++)
for (int j = 0; j < result.col; j++)
result.matrix[i][j] = k*ma.matrix[i][j];
return result;
}

这样写能编译就是用不了,等于没写。参考《C++ Primer Plus》没找到友元模板函数的参数既有模板类又有模板变量的情况。难道第一个参数k必须给出确定的int,float,double等关键字?
...全文
522 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 31 楼 qq_34291505 的回复:
[quote=引用 30 楼 hdt 的回复:] MyMatrix<int> m1; MyMatrix<int> m2; m2 = operator*<double>(10,m1); //这句话,能通过编译??? 还是 tempalte<typenmae T> MyMatrix<T> operator*(T const& k,MyMatrix<T> const& rhs) { //这里可以访问MyMatrix<U> 的私有成员?? }
那友元模板函数如何界定约束与非约束?还是友元的参数只含模板类才分,参数不仅有模板类还有模板变量就不分?[/quote] 我在#14提的改法就是operator*<T>只能访问MyMatrix<T>。
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 32 楼 xsklld 的回复:
[quote=引用 30 楼 hdt 的回复:] MyMatrix<int> m1; MyMatrix<int> m2; m2 = operator*<double>(10,m1); //这句话,能通过编译??? 还是 tempalte<typenmae T> MyMatrix<T> operator*(T const& k,MyMatrix<T> const& rhs) { //这里可以访问MyMatrix<U> 的私有成员?? }
当然是指后者。 http://coliru.stacked-crooked.com/a/2623f0546c733cd9[/quote] test忘了初始化,不过反正跟这个问题无关……
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 30 楼 hdt 的回复:
MyMatrix<int> m1; MyMatrix<int> m2; m2 = operator*<double>(10,m1); //这句话,能通过编译??? 还是 tempalte<typenmae T> MyMatrix<T> operator*(T const& k,MyMatrix<T> const& rhs) { //这里可以访问MyMatrix<U> 的私有成员?? }
当然是指后者。 http://coliru.stacked-crooked.com/a/2623f0546c733cd9
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 30 楼 hdt 的回复:
MyMatrix<int> m1; MyMatrix<int> m2; m2 = operator*<double>(10,m1); //这句话,能通过编译??? 还是 tempalte<typenmae T> MyMatrix<T> operator*(T const& k,MyMatrix<T> const& rhs) { //这里可以访问MyMatrix<U> 的私有成员?? }
那友元模板函数如何界定约束与非约束?还是友元的参数只含模板类才分,参数不仅有模板类还有模板变量就不分?
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
MyMatrix<int> m1; MyMatrix<int> m2; m2 = operator*<double>(10,m1); //这句话,能通过编译??? 还是 tempalte<typenmae T> MyMatrix<T> operator*(T const& k,MyMatrix<T> const& rhs) { //这里可以访问MyMatrix<U> 的私有成员?? }
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
引用 27 楼 xsklld 的回复:
[quote=引用 25 楼 hdt 的回复:] [quote=引用 19 楼 xsklld 的回复:] [quote=引用 8 楼 hdt 的回复:] [quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
预声明、类内声明、函数定义都在一个.h文件里面,而且没有交叉引用,就是报链接器错误,无法解析的外部命令,可能是形成了一个友元对多个实例,但是用约束声明operator* <>就报语法错误,说<前面缺少分号。更奇怪的是输出友元<<重载的类内声明写成operator<< <>显示没有函数定义,但是我在类外给了定义,而且还能调用。输入友元>>和输出友元一模一样的写法,什么错误都没有。我在怀疑VS2015有bug。[/quote]因为这个本身是模板函数,而且应该作为约束模板友元函数,模板类的约束模板友元函数需要前置声明,这是语法规定。不用前置声明的是非约束型。我两种都试过了,没用,不加前置声明编译都过不了。[/quote] 谁跟你说的,走都不利落,先不要跑 下面matrix.hpp
template<typename T>
class Matrix{
public:
	Matrix()
	{}
	Matrix(Matrix const& rhs)
	{

	}
	template <typename T>
	friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );


};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
	Matrix<T> result;
	return result;	
}
[/quote] VS太不讲究了吧,这也能过。T重复声明了。 参见标准http://www.eel.is/c++draft/temp#local-6
引用
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
顺便GCC是过不了的: http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote] operator * 的模板参数和matrix 有毛关系? 不是matrix的模板参数决定operator* 的模板参数, 因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数 因此 改一下就可以了 #include <iostream> #include <vector> template<typename T> class Matrix{ public: Matrix() {} Matrix(Matrix const& rhs) { } template <typename T1> friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs ); }; template< typename T> Matrix<T> operator*(T const& k,Matrix<T> const& rhs ) { Matrix<T> result; return result; } int main() { Matrix<int> m; Matrix<int> b; b = 10*m; } [/quote] 这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。[/quote] 你确定 ??
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 27 楼 xsklld 的回复:
[quote=引用 25 楼 hdt 的回复:] [quote=引用 19 楼 xsklld 的回复:] [quote=引用 8 楼 hdt 的回复:] [quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
预声明、类内声明、函数定义都在一个.h文件里面,而且没有交叉引用,就是报链接器错误,无法解析的外部命令,可能是形成了一个友元对多个实例,但是用约束声明operator* <>就报语法错误,说<前面缺少分号。更奇怪的是输出友元<<重载的类内声明写成operator<< <>显示没有函数定义,但是我在类外给了定义,而且还能调用。输入友元>>和输出友元一模一样的写法,什么错误都没有。我在怀疑VS2015有bug。[/quote]因为这个本身是模板函数,而且应该作为约束模板友元函数,模板类的约束模板友元函数需要前置声明,这是语法规定。不用前置声明的是非约束型。我两种都试过了,没用,不加前置声明编译都过不了。[/quote] 谁跟你说的,走都不利落,先不要跑 下面matrix.hpp
template<typename T>
class Matrix{
public:
	Matrix()
	{}
	Matrix(Matrix const& rhs)
	{

	}
	template <typename T>
	friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );


};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
	Matrix<T> result;
	return result;	
}
[/quote] VS太不讲究了吧,这也能过。T重复声明了。 参见标准http://www.eel.is/c++draft/temp#local-6
引用
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
顺便GCC是过不了的: http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote] operator * 的模板参数和matrix 有毛关系? 不是matrix的模板参数决定operator* 的模板参数, 因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数 因此 改一下就可以了 #include <iostream> #include <vector> template<typename T> class Matrix{ public: Matrix() {} Matrix(Matrix const& rhs) { } template <typename T1> friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs ); }; template< typename T> Matrix<T> operator*(T const& k,Matrix<T> const& rhs ) { Matrix<T> result; return result; } int main() { Matrix<int> m; Matrix<int> b; b = 10*m; } [/quote] 这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。[/quote]是想一个友元对一个具体化,但是像你那样加约束会报错。
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 25 楼 hdt 的回复:
[quote=引用 19 楼 xsklld 的回复:] [quote=引用 8 楼 hdt 的回复:] [quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
预声明、类内声明、函数定义都在一个.h文件里面,而且没有交叉引用,就是报链接器错误,无法解析的外部命令,可能是形成了一个友元对多个实例,但是用约束声明operator* <>就报语法错误,说<前面缺少分号。更奇怪的是输出友元<<重载的类内声明写成operator<< <>显示没有函数定义,但是我在类外给了定义,而且还能调用。输入友元>>和输出友元一模一样的写法,什么错误都没有。我在怀疑VS2015有bug。[/quote]因为这个本身是模板函数,而且应该作为约束模板友元函数,模板类的约束模板友元函数需要前置声明,这是语法规定。不用前置声明的是非约束型。我两种都试过了,没用,不加前置声明编译都过不了。[/quote] 谁跟你说的,走都不利落,先不要跑 下面matrix.hpp
template<typename T>
class Matrix{
public:
	Matrix()
	{}
	Matrix(Matrix const& rhs)
	{

	}
	template <typename T>
	friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );


};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
	Matrix<T> result;
	return result;	
}
[/quote] VS太不讲究了吧,这也能过。T重复声明了。 参见标准http://www.eel.is/c++draft/temp#local-6
引用
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
顺便GCC是过不了的: http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote] operator * 的模板参数和matrix 有毛关系? 不是matrix的模板参数决定operator* 的模板参数, 因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数 因此 改一下就可以了 #include <iostream> #include <vector> template<typename T> class Matrix{ public: Matrix() {} Matrix(Matrix const& rhs) { } template <typename T1> friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs ); }; template< typename T> Matrix<T> operator*(T const& k,Matrix<T> const& rhs ) { Matrix<T> result; return result; } int main() { Matrix<int> m; Matrix<int> b; b = 10*m; } [/quote] 这样写代表任何operator*<U>都是matrix<T>的友元,比如operator*<double>可以访问matrix<int>之类。虽然在我看来没什么区别,但从题主最开始的代码来看他似乎只希望operator*<T>能访问matrix<T>。
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 23 楼 qq_34291505 的回复:
[quote=引用 22 楼 xsklld 的回复:] 楼上是对#14的补充。
template <typename T>
		friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);

template <typename T>
		friend MyMatrix operator*(const T k, const MyMatrix & ma);
这两种写法在VS里面显示都是一样的,后面那种把光标移上去都会显示正确的备注,但是实际只有第一个能用。而且加上名称空间又不对了。[/quote] 看#19楼。 我不知道VS对这种情况是怎么处理的,语义是什么(比如operator*<double>是不是MyMatrix<int>的友元之类),你可能需要自己去查阅VS的文档。 总而言之这种写法是不规范的,至少不具备可移植性,不推荐。
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
引用 19 楼 xsklld 的回复:
[quote=引用 8 楼 hdt 的回复:] [quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
预声明、类内声明、函数定义都在一个.h文件里面,而且没有交叉引用,就是报链接器错误,无法解析的外部命令,可能是形成了一个友元对多个实例,但是用约束声明operator* <>就报语法错误,说<前面缺少分号。更奇怪的是输出友元<<重载的类内声明写成operator<< <>显示没有函数定义,但是我在类外给了定义,而且还能调用。输入友元>>和输出友元一模一样的写法,什么错误都没有。我在怀疑VS2015有bug。[/quote]因为这个本身是模板函数,而且应该作为约束模板友元函数,模板类的约束模板友元函数需要前置声明,这是语法规定。不用前置声明的是非约束型。我两种都试过了,没用,不加前置声明编译都过不了。[/quote] 谁跟你说的,走都不利落,先不要跑 下面matrix.hpp
template<typename T>
class Matrix{
public:
	Matrix()
	{}
	Matrix(Matrix const& rhs)
	{

	}
	template <typename T>
	friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );


};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
	Matrix<T> result;
	return result;	
}
[/quote] VS太不讲究了吧,这也能过。T重复声明了。 参见标准http://www.eel.is/c++draft/temp#local-6
引用
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
顺便GCC是过不了的: http://coliru.stacked-crooked.com/a/bc5630de31a84d90[/quote] operator * 的模板参数和matrix 有毛关系? 不是matrix的模板参数决定operator* 的模板参数, 因为operator* 是友元所以是operator*的模板参数决定matrix的模板参数 因此 改一下就可以了 #include <iostream> #include <vector> template<typename T> class Matrix{ public: Matrix() {} Matrix(Matrix const& rhs) { } template <typename T1> friend Matrix<T1> operator*(T1 const& k,Matrix<T1> const& rhs ); }; template< typename T> Matrix<T> operator*(T const& k,Matrix<T> const& rhs ) { Matrix<T> result; return result; } int main() { Matrix<int> m; Matrix<int> b; b = 10*m; }
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 13 楼 hdt 的回复:
说明,你提出了一个错误的问题,也就是说你的方向是错误。 因为你完全没有掌握c++的语法,所以你才会被那些旁枝末节所干扰。 大致看了一下你的贴图,猜测可能是因为那个命名空间的问题。试着把所有的命名空间删掉。 如果不行,把你的代码贴出来
我知道问题在哪了,函数定义写到名称空间外面了。
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 22 楼 xsklld 的回复:
楼上是对#14的补充。
template <typename T>
		friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);

template <typename T>
		friend MyMatrix operator*(const T k, const MyMatrix & ma);
这两种写法在VS里面显示都是一样的,后面那种把光标移上去都会显示正确的备注,但是实际只有第一个能用。而且加上名称空间又不对了。
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
楼上是对#14的补充。
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 20 楼 qq_34291505 的回复:
引用 18 楼 hdt 的回复:
额,类内声明里面MyMatrix和MyMatrix<T>语法上是一样的吧?而且字体都标成同一种颜色,为什么编译的时候前面的不通过?
你引用错楼层了吧…… 标准规定,你不加<T>就会被当成普通函数(而非模板函数)去查找,当然找不到。
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 18 楼 hdt 的回复:
额,类内声明里面MyMatrix和MyMatrix<T>语法上是一样的吧?而且字体都标成同一种颜色,为什么编译的时候前面的不通过?
xskxzr 2017-11-23
  • 打赏
  • 举报
回复
引用 8 楼 hdt 的回复:
[quote=引用 7 楼 qq_34291505 的回复:] [quote=引用 4 楼 qq_34291505 的回复:] [quote=引用 3 楼 hdt 的回复:] 既然没学会走,就要跑,肯定要摔跤 我指出一点,你的operator* 是不是和 matrix类声明 在不同的文件里,把它改成同一个。
预声明、类内声明、函数定义都在一个.h文件里面,而且没有交叉引用,就是报链接器错误,无法解析的外部命令,可能是形成了一个友元对多个实例,但是用约束声明operator* <>就报语法错误,说<前面缺少分号。更奇怪的是输出友元<<重载的类内声明写成operator<< <>显示没有函数定义,但是我在类外给了定义,而且还能调用。输入友元>>和输出友元一模一样的写法,什么错误都没有。我在怀疑VS2015有bug。[/quote]因为这个本身是模板函数,而且应该作为约束模板友元函数,模板类的约束模板友元函数需要前置声明,这是语法规定。不用前置声明的是非约束型。我两种都试过了,没用,不加前置声明编译都过不了。[/quote] 谁跟你说的,走都不利落,先不要跑 下面matrix.hpp
template<typename T>
class Matrix{
public:
	Matrix()
	{}
	Matrix(Matrix const& rhs)
	{

	}
	template <typename T>
	friend Matrix<T> operator*(T const& k,Matrix<T> const& rhs );


};
template< typename T>
Matrix<T> operator*(T const& k,Matrix<T> const& rhs )
{
	Matrix<T> result;
	return result;	
}
[/quote] VS太不讲究了吧,这也能过。T重复声明了。 参见标准http://www.eel.is/c++draft/temp#local-6
引用
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
顺便GCC是过不了的: http://coliru.stacked-crooked.com/a/bc5630de31a84d90
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
真相重于对错 2017-11-23
  • 打赏
  • 举报
回复
template <typename T>
class MyMatrix
{
private:
	int row, col;
	T ** matrix;
public:
	MyMatrix();  // default constructor
	MyMatrix(int ro, int co);  // constructor #1
	MyMatrix(int ro, int co, T ** f);  // constructor #2
	~MyMatrix() { for (int n = 0; n < row; n++) delete[] matrix[n]; delete[] matrix; };  // destructor
	MyMatrix(const MyMatrix & ma);  // copy constructor
	template <typename T> 
	friend MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma);  
};


template <typename T>
MyMatrix<T>::MyMatrix()
{
	row = 0;
	col = 0;
	matrix = nullptr;
}

template <typename T>
MyMatrix<T>::MyMatrix(int ro, int co)
{
	row = ro;
	col = co;
	matrix = new T*[row];
	for (int n = 0; n < row; n++) matrix[n] = new T[col];
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			matrix[i][j] = 0;
}

template <typename T>
MyMatrix<T>::MyMatrix(int ro, int co, T ** f)
{
	row = ro;
	col = co;
	matrix = new T*[row];
	for (int n = 0; n < row; n++) matrix[n] = new T[col];
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			matrix[i][j] = f[i][j];
}

template <typename T>
MyMatrix<T>::MyMatrix(const MyMatrix & ma)
{
	row = ma.row;
	col = ma.col;
	matrix = new T*[row];
	for (int n = 0; n < row; n++) matrix[n] = new T[col];
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			matrix[i][j] = ma.matrix[i][j];
}

template <typename T>
MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma)
{
	MyMatrix<T> result(ma.row, ma.col);
	for (int i = 0; i < ma.row; i++)
		for (int j = 0; j < ma.col; j++)
			result.matrix[i][j] = k*ma.matrix[i][j];
	return result;
} 
_iChthyosaur 2017-11-23
  • 打赏
  • 举报
回复
引用 11 楼 hdt 的回复:
解决一个问题的时候,先尽可能的把它简化。不要被旁枝末节所干扰。 你试过我的代码了吗?
你那个是能运行,但是我这个和你一样就是跑不了。
引用 13 楼 hdt 的回复:
说明,你提出了一个错误的问题,也就是说你的方向是错误。 因为你完全没有掌握c++的语法,所以你才会被那些旁枝末节所干扰。 大致看了一下你的贴图,猜测可能是因为那个命名空间的问题。试着把所有的命名空间删掉。 如果不行,把你的代码贴出来
删了,也没用。报多个”*“运算符匹配,加<>约束会触发异常。 template <typename T> class MyMatrix { private: int row, col; T ** matrix; public: MyMatrix(); // default constructor MyMatrix(int ro, int co); // constructor #1 MyMatrix(int ro, int co, T ** f); // constructor #2 ~MyMatrix() { for (int n = 0; n < row; n++) delete[] matrix[n]; delete[] matrix; }; // destructor MyMatrix(const MyMatrix & ma); // copy constructor template <typename T> friend MyMatrix operator*(const T k, const MyMatrix & ma); }; template <typename T> MyMatrix<T>::MyMatrix() { row = 0; col = 0; matrix = nullptr; } template <typename T> MyMatrix<T>::MyMatrix(int ro, int co) { row = ro; col = co; matrix = new T*[row]; for (int n = 0; n < row; n++) matrix[n] = new T[col]; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) matrix[i][j] = 0; } template <typename T> MyMatrix<T>::MyMatrix(int ro, int co, T ** f) { row = ro; col = co; matrix = new T*[row]; for (int n = 0; n < row; n++) matrix[n] = new T[col]; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) matrix[i][j] = f[i][j]; } template <typename T> MyMatrix<T>::MyMatrix(const MyMatrix & ma) { row = ma.row; col = ma.col; matrix = new T*[row]; for (int n = 0; n < row; n++) matrix[n] = new T[col]; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) matrix[i][j] = ma.matrix[i][j]; } template <typename T> MyMatrix<T> operator*(const T k, const MyMatrix<T> & ma) { MyMatrix<T> result(ma.row, ma.col); for (int i = 0; i < ma.row; i++) for (int j = 0; j < ma.col; j++) result.matrix[i][j] = k*ma.matrix[i][j]; return result; }
加载更多回复(14)

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧