VS2008与DEV C++的区别

xiaoshi935 2009-05-26 09:40:09
运行了一个程序,发现在DEV下可以运行,而拿到VS2008下提示如下错误,真是莫名其妙:
“<”: 有符号/无符号不匹配
error C2057: 应输入常量表达式
error C2466: 不能分配常量大小为 0 的数组
error C2133: “numResult”: 未知的大小
error C2057: 应输入常量表达式
error C2466: 不能分配常量大小为 0 的数组
error C2133: “numResult”: 未知的大小

程序如下:

/*
* exe1102.cpp
* 用类的方法进行求解下题
*
* A + B Problem
*
* Input
* The input will consist of a series of pairs of integers a and b, separated by one line.
* Each integer may consist of less than 79 digits.
*
* Output
* For each pair of input integers a and b you should output the sum of a and b in one line
*
* Sample input
* 1
* 5000000000000000000000000000000000000000000000000000000000
* -1
* 23
*
* Sample output
* 5000000000000000000000000000000000000000000000000000000001
* 22
*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class BigInt { //定义大整数类
string s;
int sign;
public:
BigInt (const string &num);
BigInt (int sign, const string &value);
~BigInt () {}
static string numberArrayToString (int a[], int length);
static int numValueCompare (const string &a, const string &b);
string getNumValue () { return s; };
int getSign () { return sign; }
friend BigInt operator + (const BigInt a, const BigInt b); //重载操作符+
// Pre-Condition: a >= b >= 0
friend BigInt operator - (const BigInt a, const BigInt b); //重载操作符-
friend ostream &operator << (ostream &o, const BigInt &a); //重载输出操作符
};

BigInt::BigInt (const string &num) {
if (num[0] == '-') {
sign = -1;
s = num.substr( 1);
} else {
sign = 1;
s = num;
}
}

BigInt::BigInt (int sign, const string &value) {
this->sign = sign;
s = value;
}

string BigInt::numberArrayToString (int a[], int length) {
string result = "";
int i = 0;
while (i < length-1 && a[i] == 0)
++i;
while (i < length) {
result += (char)(a[i] + '0');
++i;
}
return result;
}

int BigInt::numValueCompare (const string &a, const string &b) {
if (a.size() < b.size()) return -1;
if (a.size() > b.size()) return 1;

for (int i= 0; i < a.size(); ++i) {
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return 1;
}
return 0;
}

// Pre-Condition: a >= b >= 0
BigInt operator - (const BigInt a, const BigInt b) {
int shortLen = b.s.size();
int longLen = a.s.size();
string shortNum = b.s;
string longNum = a.s;

int numResult[longLen];

int temp, i, j;
int rent = 0;
for (i= longLen-1, j= shortLen-1; i >= 0 && j >= 0; i--, j--) {
temp = (longNum[i] - '0') - (shortNum[j] - '0') - rent;
rent = ((temp < 0) ? 1 : 0);
numResult[i] = temp + rent * 10;
}

while (i >= 0) {
temp = (longNum[i] - '0') - rent;
rent = ((temp < 0) ? 1 : 0);
numResult[i] = temp + rent * 10;
--i;
}

return BigInt( 1, BigInt::numberArrayToString( numResult, longLen));
}

BigInt operator + (const BigInt a, const BigInt b) {
if (a.sign == b.sign) {
int shortLen, longLen;
string shortNum, longNum;
if (a.s.size() <= b.s.size()) {
shortLen = a.s.size();
longLen = b.s.size();
shortNum = a.s;
longNum = b.s;
} else {
shortLen = b.s.size();
longLen = a.s.size();
shortNum = b.s;
longNum = a.s;
}

int numResult[longLen+1];

int temp, i, j;
int carry = 0;
for (i= longLen-1, j= shortLen-1; i >= 0 && j >= 0; i--, j--) {
temp = longNum[i] - '0' + shortNum[j] - '0' + carry;
numResult[i+1] = temp % 10;
carry = temp / 10;
}
while (i >= 0) {
temp = longNum[i] - '0' + carry;
numResult[i+1] = temp % 10;
carry = temp / 10;
i--;
}
numResult[0] = carry;
return BigInt( a.sign, BigInt::numberArrayToString( numResult, longLen+1));
}

// now a and b in different sign
int compareResult = BigInt::numValueCompare( a.s, b.s );
if (compareResult == 0)
return BigInt( 1, string( "0" ));
if (compareResult < 0)
return BigInt( b.sign, (BigInt( 1, b.s) - BigInt( 1, a.s)).getNumValue());
if (compareResult > 0)
return BigInt( a.sign, (BigInt( 1, a.s) - BigInt( 1, b.s)).getNumValue());
}

ostream &operator << (ostream &o, const BigInt &a) {
return o <<(a.sign == -1 ? "-" : "") <<a.s;
}

int main () {
BigInt a("500000000000000000009999999");
BigInt b("600000000000000000009999999");
cout << a + b << endl;
BigInt c("-500000000000000000009999999");
BigInt d("600000000000000000009999999");
cout << c + d << endl;
}


其实也就是那个BigInt operator - 和BigInt operator + 里面的数组 int numResult[longLen]和 int numResult[longLen+1]的问题。
通过这个程序感觉好像VS2008好像一个成员函数中定义数组之前必须分配大小,而不是参数的大小,比方此题中的longLen = a.s.size();
这个究竟是什么问题呢,该怎么改呢?
...全文
714 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sandy_zc_1 2009-05-27
  • 打赏
  • 举报
回复
DevCpp用的是GCC的编译器,GCC编译器符合GNU C++的标准,但是不一定完全符合C++语言的规范。LZ的问题就是使用了GCC提供的一些扩展特性,而标准C++是不支持这个东西的。

这种动态的数组大小,应该用new分配,用完了再用delete回收。
amossavez 2009-05-27
  • 打赏
  • 举报
回复
进来看看!
Sou2012 2009-05-26
  • 打赏
  • 举报
回复
MARK!!
s510601017 2009-05-26
  • 打赏
  • 举报
回复
cv2008是安C#编辑器,不是VC6.0的标准。
fairchild811 2009-05-26
  • 打赏
  • 举报
回复
vs2008支持的标准一些.devcpp老了,好像好久没更新了
Kusk 2009-05-26
  • 打赏
  • 举报
回复
C99支持动态长度数组,但C++标准不支持。
fox000002 2009-05-26
  • 打赏
  • 举报
回复
lz 在无意中用了编译器的扩展

但是不是每个编译器都吃这一套
lingyin55 2009-05-26
  • 打赏
  • 举报
回复
vs2008对于类型匹配的检查相对其它编译器都要严格。
lingyin55 2009-05-26
  • 打赏
  • 举报
回复
for (unsigned int i= 0; i < a.size(); ++i) {////这里把i定义成unsigned就行
if (a[i] < b[i]) return -1;
if (a[i] > b[i]) return 1;
}
taodm 2009-05-26
  • 打赏
  • 举报
回复
哎,定义数组要用常量。c++9标准就这么规定了的。基础呀。
lingyin55 2009-05-26
  • 打赏
  • 举报
回复
int numResult[longLen];
不是动态定义的数组在编译期间就要求知道数组的大小。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧