Getting error C2177: constant too big with MPIR

823 Views Asked by At

Running on Widows 7 with MSVS 2010

I am following this tutorial to understand how to use MPIR library for adding two big integers

I understand this library should help me in adding very big numbers as shown in the program below:

#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";


} 

But still I get error C2177: constant too big. Did I misunderstand MPIR ?

1

There are 1 best solutions below

9
On

Such constant is (very likely) too big for standard integer types. You should use char * constructor instead:

void mpz_class::mpz_class (const char *s)

For example:

mpz_class answer_a("111111111111111111111111111111111111111111111111");

to make this work you need to include suitable MPIR C++ interface header (notice that <gmpxx.h> is from C++ interface of GNU MP library):

#include <mpirxx.h>

See 12.2 C++ Interface Integers chapter in MPIR documentation for more details.