我刚刚在重载类 FeatureRandomCounts 的赋值运算符时遇到段错误,该类的指针成员为 _rects,指向 FeatureCount 数组,大小为 rhs._dim,其他日期成员为非指针:
FeatureRandomCounts & FeatureRandomCounts::operator=(const FeatureRandomCounts &rhs)
{
if (_rects) delete [] _rects;
*this = rhs; // segment fault
_rects = new FeatureCount [rhs._dim];
for (int i = 0; i < rhs._dim; i++)
{
_rects[i]=rhs._rects[i];
}
return *this;
}
有人知道吗?谢谢和问候!
最佳答案
*this = rhs;
调用 operator=(),这是您正在编写的函数。提示无限递归、堆栈溢出、崩溃。
此外,如果您使用 std::vector 而不是 C 风格的数组,您可能根本不需要实现 operator=()。
关于c++ - 重载运算符中的段错误 =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1996548/