运算符重载实现:0xC0000005:读取位置存在访问冲突
Operator overloading implementation: 0xC0000005: Access violation reading location
当在我的BigInt实现中执行return BigInt(*this) += other;
时,会抛出错误0xC0000005: Access violation reading location (insert memory location here)
,并关闭程序。它为什么这么做有什么帮助吗?对于我的程序的上一个上下文,请转到复制构造函数问题C++:";0xC0000005:访问违规写入位置0x00000000;
BigInt.cpp
// binary addition
BigInt BigInt::operator+(BigInt const& other) const {
return BigInt(*this) += other;
}
// compound addition-assignment operator
BigInt BigInt::operator+=(BigInt const& other) {
//return this->data = this->data + other.data;
//BigInt thisBigInt = *this;
if (!other.isPositive) {
//return thisBigInt -= other;
}
//possible check for both negative???
int sum = 0; //holds the sum of the value in both vectors
int maxSize = 0; //holds size of biggest BigInt
int carry = 0; //holds carry over value
int sizeDifference = 0; //holds size difference between b and a if b is bigger
//check size
while (bigIntVector->getSize() < other.bigIntVector->getSize()) {
bigIntVector->resize(); //increase size of first big int until it matches size of second
}
if (bigIntVector->getSize() > other.bigIntVector->getSize()) {
sizeDifference = bigIntVector->getSize() - other.bigIntVector->getSize();
//cout << "sizeDiff: " << sizeDifference << endl;
}
maxSize = bigIntVector->getSize();
int otherCounter = other.bigIntVector->getSize() - 1; //keeps track if we are done getting digits from other array
//cout << "otherCounter: " << otherCounter << endl;
for (int i = maxSize - 1; i >= 0; i--) {
//cout << "element1: " << bigIntVector.getElementAt(i) << endl;
//cout << "element2: " << other.bigIntVector.getElementAt(i) << endl;
sum += bigIntVector->getElementAt(i);
if (otherCounter >= 0) {
sum += other.bigIntVector->getElementAt(i - sizeDifference); //move index if size is different
sum += carry;
carry = 0;
//cout << "sum: " << sum << endl;
if (sum > 9) {
++carry;
bigIntVector->setElementAt(i, sum%base);
}
else {
carry = 0;
bigIntVector->setElementAt(i, sum%base);
}
--otherCounter; //only decrement otherCounter if we have reached 2nd vector elements
}
if (otherCounter < 0 && carry > 0) {
bigIntVector->resize(); //increase size of big int
bigIntVector->setElementAt(i, carry); //set carry in front of sum spot
}
sum = 0;
}
return *this;
}
您从operator+=
按值返回BigInt
,这会创建一个副本,而且您可能没有指定正确的副本构造函数来照顾或内部动态分配的内存,您应该返回一个引用:
BigInt& BigInt::operator+=(BigInt const& other)
相关文章:
- C++模板来检查友元函数的存在
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 既然存在危险,为什么项目要使用-I include开关
- 写入位置0x0000000C时发生访问冲突
- 我们可以访问一个不存在的联盟的成员吗
- 将文件复制到自定义位置,存在字符串转换问题
- ifstream::is_open 返回 true,即使该位置不存在文件也是如此
- 全局倾角定义的位置/如何存在哪些其他类似功能
- Excel VBA中C++DLL位置存在无法解释的行为
- C++可能存在写入位置的构造函数错误冲突
- qt检查文件中是否存在文件,如果它没有提示用户输入其位置,则将文件复制到程序工作目录
- STL中set的insert函数中为什么存在位置参数
- 强制 cmake 选择依赖位置(如果存在多个)
- 在 C++/CLI Visual Studio C++ 中以指定的数组位置将文本框中的值保存在 char 中
- 单击按钮时,将当前位置/订单保存在QTabWidget中 - 用户设置
- 运算符重载实现:0xC0000005:读取位置存在访问冲突
- 检查STL vector中某个位置的元素是否存在(已设置)
- 实现一个 dinamic 数组(我可以访问不存在的位置)
- 如何以最快的方式检查两个给定位置之间是否存在一个数字