我正在尝试重载+
,但我收到了错误:
@Error1 Error: no [] operator overload for type main.Matrix
此外,我在测量时间时也遇到了错误。
import std.stdio;
import std.c.process;
import std.date;
//@Error2
class Matrix
{
Matrix opBinary(string op)(Matrix another)
{
if(op == "+")
{
if (row != another.row || col != another.col)
{
// error
getchar();
return (this);
}
Matrix temp = new Matrix(row, col);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
temp[i][j] = this[i][j] + another[i][j];
//@Error1
return temp;
}
}
};
答案 0 :(得分:1)
m2[i][j] = this[i][j] + b[i][j];
您必须定义opIndex
才能使用此类操作。 E.g:
double[] opIndex(size_t i1)
{
return d[i1];
}
double opIndex(size_t i1, size_t i2)
{
return d[i1][i2];
}
或者只是在该方法内部,您可能希望直接访问double[][]
:
m2.d[i][j] = this.d[i][j] + b.d[i][j];
std.date.d_time starttime = getCount();
使用StopWatch。 E.g:
StopWatch sw;
sw.start();
// operations...
sw.stop();
writefln("elapsed time = %s", sw.peek().msecs);