如何在 Ada 中添加向量

Ben*_*Ben 2 vector ada gnat-gps

我有一个 (x,y,x) 形式的向量表示坐标。我希望能够执行 (x,y,z) + (x2,y2,z2) 之类的操作来生成一组新坐标。Ada 说它不能对复合类型使用“+”,但肯定有办法做到这一点吗?

Sim*_*ght 6

如果你有

type Vector is record
   X : Float;
   Y : Float;
   Z : Float;
end record;
Run Code Online (Sandbox Code Playgroud)

你可以定义+

function "+" (L, R : Vector) return Vector is
  (L.X + R.X, L.Y + R.Y, L.Z + R.Z);
Run Code Online (Sandbox Code Playgroud)

当你定义-类似地使用时要小心-!这个错误是非常难以发现。