在Matrix类中不能重载运算符多吗? [英] Can not overload operator mutiple in Matrix class?
本文介绍了在Matrix类中不能重载运算符多吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想重载operator *(mutiple),这是我的代码:
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Drawing.Drawing2D;
命名空间 WindowsFormsApplication1
{
public class Matrix
{
// 属性
public int nRow;
public int nCol;
public double [,] PhTu;
// 构造函数
public Matrix( int Ro, int Co)
{
< span class =code-keyword> this .nRow = Ro;
this .nCol = Co;
此 .PhTu = new double [Ro,Co];
}
public static Matrix 运算符 *(矩阵A)
{
如果(A.nRow == B.nRow&& ; A.nCol == B.nCol)
{
Matrix C = new Matrix(A.nRow,A.nCol);
int i,j;
for (i = 0 ; i < ; A.nRow; i ++)
for (j = 0 ; j < A.nCol; j ++)
C.PhTu [i,j] = A.PhTu [i,j] + B.PhTu [i, D];
return C;
}
else
{
MessageBox.Show( 两个矩阵的大小不同!);
return null ;
}
}
}
public partial < span class =code-keyword> class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender,EventArgs e)
{
Matrix A = new Matrix( 2 , 1 );
A.PhTu [ 0 , 0 ] = 1 跨度>;
A.PhTu [ 1 , 0 ] = 3 跨度>;
矩阵B = 新矩阵( 1 , 2 跨度>);
B.PhTu [ 0 , 0 ] = -1;
B.PhTu [ 0 , 1 ] = 3 跨度>;
Matrix C;
C = A * B;
for ( int i = 0 ; i < C.nRow; i ++)
for ( int j = 0 ; j < C.nCol; j ++)
{
MessageBox.Show(C.PhTu [i,j] .ToString());
}
}
}
}
但我不能重载(该代码不起作用)。这该怎么做?谢谢。
解决方案
静态运算符必须包含运算符的两边,你的定义应该是:
public static Matrix 运算符 *(矩阵A,矩阵B)
{
// .. 。
}
否则,您希望B来自静态方法?
I want to overload operator * (mutiple), this is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication1
{
public class Matrix
{
// Properties
public int nRow;
public int nCol;
public double[,] PhTu;
// Constructor
public Matrix(int Ro, int Co)
{
this.nRow = Ro;
this.nCol = Co;
this.PhTu = new double[Ro, Co];
}
public static Matrix operator *(Matrix A)
{
if (A.nRow == B.nRow && A.nCol == B.nCol)
{
Matrix C = new Matrix(A.nRow, A.nCol);
int i, j;
for (i = 0; i < A.nRow; i++)
for (j = 0; j < A.nCol; j++)
C.PhTu[i, j] = A.PhTu[i, j] + B.PhTu[i, j];
return C;
}
else
{
MessageBox.Show("Two matrixes are not the same size!");
return null;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Matrix A = new Matrix(2, 1);
A.PhTu[0, 0] = 1;
A.PhTu[1, 0] = 3;
Matrix B = new Matrix(1, 2);
B.PhTu[0, 0] = -1;
B.PhTu[0, 1] = 3;
Matrix C;
C = A * B;
for (int i = 0; i < C.nRow; i++)
for (int j = 0; j < C.nCol; j++)
{
MessageBox.Show(C.PhTu[i, j].ToString());
}
}
}
}
But I cant overload (that code is not working). How to do this? Thanks.
解决方案
Static operators have to include both sides of the operator, your definition should be:
public static Matrix operator *(Matrix A, Matrix B) { //... }
Otherwise, where did you expect B to come from in a static method?
这篇关于在Matrix类中不能重载运算符多吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文