2015-11-07 105 views
-2

我在使用我的BigInt類工作時遇到了一些麻煩,讓我的拷貝構造函數工作。複製構造函數問題C++:「0xC0000005:寫入位置0x00000000的訪問衝突」。

在拷貝構造函數BigIntVector.cpp,該行:

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

導致異常0xC0000005: Access violation writing location 0x00000000.任何弄清楚爲什麼將不勝感激幫助。謝謝。

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... 

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; 
} 

BigIntVector是一個自定義Vector類。與STL向量相比。

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

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; 
} 
+2

你傳遞參考orig。你應該使用'orig.bigIntVector' – Nandu

回答

1

你提到過原稿。你應該使用'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) 
+0

你在代碼中指的是哪裏? – sanic

+0

this-> bigIntVector = new BigIntVector *(orig.bigIntVector);返回錯誤,因爲它們不能分配給彼此。 – sanic

+1

this-> bigIntVector = new BigIntVector(*(orig.bigIntVector));調用BigIntVector的複製構造函數 – Nandu

相關問題