变量周围的堆栈已损坏

Stack around variable is corrupted

本文关键字:已损坏 堆栈 周围 变量      更新时间:2023-10-16

我知道这个主题有很多问题,但它们似乎都特定于代码。

我有这个功能——

Point2 ITCS4120::operator* (const Matrix3x3& m, const Point2& p) {
   Point2 result;
   for(int i=0;i<3;i++) {
       result[i] = (m[i][0]*p[0]) + (m[i][1]*p[1]) + (m[i][2]);
       }
   return result; //error here
   }

它在 return 语句上给了我一个错误,说"运行时检查失败 #2 - 变量'结果'周围的堆栈已损坏。

我看不出该功能有什么问题。Matrix3x3的数组只是 -

float array[3][3];

而 Point2 的数组是

float array [2];

Matrix3x3 和 Point2 类都有这样的代码:

/** Write access for element in row [i] */
inline Scalar* operator[](int i) {return array[i];}
/** Read access for element in row [i] */
inline const Scalar* operator[](int i)const {return array[i];}

这段代码是给我的,我之前有一些家庭作业要用点、矩阵和向量做算术。我的代码通过了所有的测试,所以我假设我的Point2 ITCS4120::operator*(const Matrix3x3&m,const Point2&p)代码是正确的。但也许我需要以不同的方式使用 [] 运算符?

似乎你的观点包含两个float,但你正在写三个。