incompatible types!

Caligo iteronvexor at gmail.com
Tue Apr 5 10:23:04 PDT 2011


import std.conv : to;

struct Matrix(T, size_t r, size_t c){

public:
  T[r*c] _data;

  Matrix opBinary(string op)(T scalar) if(op == "*"){
    string construct(){
      string result = "[";
      for(size_t i = 0; i < r * c; i++)
	result ~= "_data["~to!string(i)~"] * scalar, ";
      return result ~= "]";
    }
    return Matrix(mixin(construct()));
  }

  Matrix opBinary(string op)(ref const(Matrix) m)
    const if(op == "+" || op == "-"){
    string construct(){
      string result = "[";
      for(size_t i = 0; i < r * c; i++)
	result ~= "_data["~to!string(i)~"]"~op~"m._data["~to!string(i)~"], ";
      return result ~= "]";
    }
    return Matrix(mixin(construct()));
  }
}

struct A(T){

public:
  Matrix!(T, 3, 1) _a;
  alias _a this;

  this(T a, T b, T c){
    this._data[0] = a;
    this._data[1] = b;
    this._data[2] = c;
  }
}


void main(){

  auto a = A!double(3, 3, 3);

  auto r1 = a * 3.0;  // ok
  auto r2 = a + a;   // ok
  auto r3 = a + (a * 3.0);  // Error: incompatible types for ((a) +
(a._a.opBinary(3.0e+0))): 'A!(double)' and 'Matrix!(double,3,1)'
}


Why does the last expression fail? and how do I fix it?


More information about the Digitalmars-d-learn mailing list