2012-02-23 135 views
0

我無法運行我的非常裸露的C++程序。它是一個BigInt類,它接受一個字符串作爲輸入,並將每個單獨的數字設置爲一個動態數組。我到目前爲止所要做的只是輸入一個BigInt,然後輸出它......非常簡單。我的程序編譯並運行得很好,但是一旦我輸入第一個輸入,它就會給我這個奇怪的錯誤......「這個應用程序要求運行時以不同尋常的方式終止它。 請聯繫應用程序的支持團隊獲取更多信息。奇怪的運行時錯誤

我不知道該怎麼解決這個問題,因爲我在代碼中找不到任何缺陷。

任何人有任何想法?

繼承人我的代碼:

頭文件:

#ifndef BIGINT_H 
#define BIGINT_H 

#include <iostream> 

namespace JHall{ 
class BigInt { 
public: 
    BigInt(std::string s = ""); 
    BigInt(const BigInt& b); 
    ~BigInt(); 
    void operator =(const BigInt& b); 
    friend std::istream& operator >>(std::istream& in, BigInt& b); 
    friend std::ostream& operator <<(std::ostream& out, const BigInt& b); 

private: 
    short* num; 
    int cap; 
    int size; 

}; 
} 
#endif /* BIGINT_H */ 

實現文件:

#include "BigInt.h" 
#include <cstdlib> 

namespace JHall{ 
BigInt::BigInt(std::string s) 
{ 
    size = s.length(); 
    cap = 100; 
    num = new short[cap]; 
    int place = 0; 
    for(int i = size-1; i >= 0; i--) 
     num[place++] = strtol(s.substr(i-1,1).c_str(), NULL, 10); 
} 

BigInt::BigInt(const BigInt& b) 
{ 
    size = b.size; 
    cap = b.cap; 
    for(int i = 0; i < size; i++) 
     num[i] = b.num[i]; 
} 

BigInt::~BigInt() 
{ 
    delete [] num; 
} 

void BigInt::operator =(const BigInt& b) 
{ 
    if(cap != b.cap) 
    { 
     short* temp = new short[b.cap]; 
     for(int i = 0; i < b.cap; i++) 
      temp[i] = b.num[i]; 
     delete [] num; 
     num = temp; 
    } 

    for(int i = 0; i < cap; i++) 
     num[i] = b.num[i]; 

    size = b.size; 
    cap = b.cap; 
} 

std::istream& operator>>(std::istream& in, BigInt& b) 
{ 
    std::string s; 
    in>>s; 
    b.size = s.length(); 
    int count = 0; 
    for(int i = b.size-1; i >= 0; i--) 
     b.num[count++] = strtol(s.substr(i-1,1).c_str(),NULL,10); 
    return in; 
} 

std::ostream& operator<<(std::ostream& out, const BigInt& b) 
{ 
    for(int i = b.size-1; i >= 0; i--) 
     out<<b.num[i]; 
    return out; 
} 

} 

主文件:

#include <cstdlib> 
#include "BigInt.h" 

using namespace std; 
using namespace JHall; 

/* 
* 
*/ 
int main(int argc, char** argv) 
{ 
    BigInt b1, b2; 
    cout<<"Enter a large integer"<<endl; 
    cin>>b1; 
    cout<<"Enter another large integer"<<endl; 
    cin>>b2; 

    cout<<b1; 
    cout<<b2; 


    return 0; 
} 
+2

呀,在調試器中運行它,看看它打破。 – 2012-02-23 01:05:19

+0

當它碰到我的第一個cin >> b1時就會中斷;我不太清楚爲什麼,因爲我已經正確地重載了​​>>操作符來閱讀我的BigInt – user1227202 2012-02-23 01:10:27

回答

3

此行是錯誤的:

for(int i = b.size-1; i >= 0; i--) 
    b.num[count++] = strtol(s.substr(i-1,1).c_str(),NULL,10); 

在i等於零時,您要求的子字符串從-1開始。

+0

啊我很愚蠢,因爲沒有看到!非常感謝! – user1227202 2012-02-23 01:14:19

+0

好點(雖然你實際上引用了兩行;-) – 2012-02-23 01:16:58

0

一個明顯的錯誤是,你不在拷貝構造函數中分配內存:如果這是每一次使用,你很可能會崩潰。另一個相對明顯的錯誤是,你認爲最多有100位數似乎是不必要的限制。賦值運算符似乎在通常複製兩次值時做了不必要的工作。此外,使用子字符串和strtol()將個人數字從char轉換爲short似乎有點矯枉過正。正確的方法是使用

int value = s[i] - '0'; 

此外,你應該考慮使用你的構造函數的成員初始化列表。當然,如果你有一個工作拷貝構造函數,你可能也想實現一個swap()方法,然後在你的賦值操作符使用:

BigInt& BigInt::operetor= (BigInt const& other) { 
    BigInt(other).swap(*this); 
    return *this; 
} 
+0

謝謝你的這些有用的意見。根據你所說的,我一定會修復我的代碼。我很感興趣的是你如何爲角色分配整數值。我從來沒有見過這個,並沒有意識到這甚至是可能的,非常感謝! – user1227202 2012-02-23 01:19:39

+0

我可以問一下它的工作原理嗎? (將字符賦給整數) – user1227202 2012-02-23 01:21:57

+0

'char'只是整數。 「0」是數字序列的基本值,即其他數字保證具有從「0」開始的序列值。也就是說,'1'=='0'+ 1',''2'=='0'+ 2'等等。您可以利用這個明顯的轉換:'1 =='1' - '0'等 – 2012-02-23 01:26:08