2012-02-23 134 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