使用 MSVS 2010 在 Widows 7 上运行
我正在关注this tutorial了解如何使用MPIR library用于添加两个大整数
我知道这个库应该帮助我添加非常大的数字,如下面的程序所示:
#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>
using namespace std;
void main(int argc, char *argv[])
{
mpz_class answer_a = 111111111111111111111111111111111111111111111111;
mpz_class answer_b = 111111111111111111111111111111111111111111111111;
mpz_class answer_c;
answer_c= answer_b + answer_a ;
cout << answer_c<<"\n";
}
但我仍然收到错误 C2177:常量太大
。
难道我理解错了MPIR ?
最佳答案
这样的常量(很可能)对于标准整数类型来说太大了。您应该使用char *
改为构造函数:
void mpz_class::mpz_class (const char *s)
例如:
mpz_class answer_a("111111111111111111111111111111111111111111111111");
要完成此工作,您需要包含合适的 MPIR C++ 接口(interface)头文件(请注意 <gmpxx.h>
来自 GNU MP 库的 C++ 接口(interface)):
#include <mpirxx.h>
有关更多详细信息,请参阅 MPIR 文档中的 12.2 C++ 接口(interface)整数章节。
关于c++ - 出现错误 C2177 : constant too big with MPIR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27565662/