简体   繁体   English

复制构造函数问题 C++:“0xC0000005:访问冲突写入位置 0x00000000。”

[英]Copy Constructor Issue C++: "0xC0000005: Access violation writing location 0x00000000."

I'm having some trouble getting my copy constructor to work in my BigInt class I'm working on.我在让我的复制构造函数在我正在处理的BigInt类中工作时遇到了一些麻烦。

In BigIntVector.cpp in the copy constructor, the lines:在复制构造函数的BigIntVector.cpp中,这些行:

    for (int i = 0; i < vectorSize; i++) {
        vectorArray[i] = orig.vectorArray[i];
    }

causes the exception 0xC0000005: Access violation writing location 0x00000000.导致异常0xC0000005: Access violation writing location 0x00000000. Any help figuring out why would be appreciated.任何帮助找出原因将不胜感激。 Thank you.谢谢你。

In BigInt.cpp :BigInt.cpp

// copy constructor
BigInt::BigInt(BigInt const& orig) 
 : isPositive(orig.isPositive)
, base(orig.base)
, skip(orig.skip) 
{
    this->bigIntVector = new BigIntVector(*orig.bigIntVector);
}

// constructor where operand is a long
BigInt::BigInt(long num) {
    base = 10;

    long sizeOfLong = 0; //holds size of num
    long tempNum = num;

    //get size of num
    if (tempNum == 0) {
        sizeOfLong = 1;
    }
    while (tempNum != 0)
    {
        tempNum /= 10;
        ++sizeOfLong;
    }

    //resize vector to match size of long
    this->bigIntVector = new BigIntVector(sizeOfLong);

    //cout << "sizeVec: " << bigIntVector.getSize() << endl;

    if (num < 0) {
        isPositive = false;
        num *= -1;
    }
    else {
        isPositive = true;
    }
    long pushedNum;
    //cout << "num: " << num << endl;
    for (int i = sizeOfLong - 1; i >= 0; --i) {
        pushedNum = (long)(num%base);
        bigIntVector->setElementAt(i, pushedNum);
        num /= base;
    }
}

// destructor
BigInt::~BigInt() {
    delete[] this->bigIntVector;
}

// binary addition
BigInt BigInt::operator+(BigInt const& other) const {

    BigInt temp(*this);
    return temp += other;
}

//more code...

My BigInt.h :我的BigInt.h

class BigInt {
private:
    BigIntVector *bigIntVector;
    bool isPositive;
    int base;
    unsigned int skip;

public:
    BigInt(BigInt const& orig);
    BigInt(long num);
    ~BigInt();
    BigInt operator+(BigInt const& other) const;
    BigInt operator+() const;
    BigInt operator++();
    BigInt operator++(int dummy);
    BigInt operator+=(BigInt const& other);
    BigInt BigInt::operator-(BigInt const& other) const;
    BigInt BigInt::operator-=(BigInt const& other);
    bool operator==(BigInt const& other) const;
    friend std::ostream & operator<<(std::ostream& os, BigInt& num);
};
inline BigInt operator+(long num, BigInt const& val) {
    return val + num;
}
inline bool operator==(long num, BigInt const& val) {
    return val == num;
}

My BigIntVector is a custom Vector class.我的BigIntVector是一个自定义的Vector类。 Compared to STL Vector.与 STL 向量相比。

The constructors in BigIntVector.cpp : BigIntVector.cpp的构造BigIntVector.cpp

// copy constructor
BigIntVector::BigIntVector(BigIntVector const& orig)
    : vectorSize(orig.vectorSize)
{
    for (int i = 0; i < vectorSize; i++) {
        vectorArray[i] = orig.vectorArray[i];
    }
}

BigIntVector::~BigIntVector() {
    delete[] vectorArray;
}

//default constructor
BigIntVector::BigIntVector()
{
    vectorSize = 1;

    //vectorArray = (long *)malloc(10 * sizeof(long));
    vectorArray = new long[vectorSize];
    for (long i = 0; i < vectorSize; i++) {
        vectorArray[i] = 0;
    }
}

//constructor that initializes a custom size for vector
BigIntVector::BigIntVector(long initialSize)
{
    vectorSize = initialSize;

    //vectorArray = (long *)malloc(initialSize*sizeof(long));
    vectorArray = new long[vectorSize];
    for (long i = 0; i < initialSize; i++) {
        vectorArray[i] = 0;
    }
}

//more code

main.cpp : main.cpp :

int main(void) {

    // object with explicit constructor from long
    BigInt num1(12);

    cout << "num1 (12): " << num1 << endl;

    // object with implicit constructor from long
    BigInt num2 = 19;

    cout << "num2 (19): " << num2 << endl;

    // binary addition BigInt+BigInt
    BigInt num3 = num1 + num2;

    cout << "num3 (31): " << num3 << endl;

    BigInt numA = 99999;

    cout << "numA (99999): " << numA << endl;

    BigInt numB = 99999;

    cout << "numB (99999): " << numB << endl;

    BigInt numC = numA + numB;

    cout << "numC (199998): " << numC << endl;

    return EXIT_SUCCESS;
}

you are passing reference to orig.您正在传递对 orig 的引用。 you should use 'orig.bigIntVector'你应该使用'orig.bigIntVector'

BigInt::BigInt(BigInt const& orig) 
 : isPositive(orig.isPositive)
, base(orig.base)
, skip(orig.skip) 
{
    this->bigIntVector = new BigIntVector(*(orig.bigIntVector));
}

should use *(orig.bigIntVector)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 0xC0000005:访问冲突读取位置 0x00000000。 C++ vs2015 - 0xC0000005: Access violation reading location 0x00000000. c++ vs2015 C ++:0xC0000005:访问冲突写入位置0x00000000 - C++: 0xC0000005: Access violation writing location 0x00000000 : 0xC0000005: 访问冲突写入位置 0x00000000 C++ 与内联汇编 - : 0xC0000005: Access violation writing location 0x00000000 C++ with inline assembly 0xC0000005:访问冲突执行位置0x00000000。 尝试访问多图的插入功能时 - 0xC0000005: Access violation executing location 0x00000000. When trying to access the insert function of multimap 0xC0000005:访问冲突读取位置 0x00000000。 重载 == 运算符的问题 - 0xC0000005: Access violation reading location 0x00000000. issues with overloaded == operator 0xC0000005:访问冲突执行位置0x00000000。 (OpenGL的) - 0xC0000005: Access violation executing location 0x00000000. (OpenGL) 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM