GNU_MP, Undefine reference to

The below friends operator are causing the errors like undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, __mpz_struct*)'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef __gmp_bigint_h
#define __gmp_bigint_h

#ifdef USE_GMP

#include "../config.h"

#include <iostream>
#include <vector>
#include <gmp.h>

class gmp_bigint
   {
private:
   static gmp_randstate_t state;
   static bool state_initialized;
   mpz_t value;

.....

.....

   /*! \name Constructors / Destructors */
   //! Default constructor
   explicit gmp_bigint(signed long int x = 0)
      {
      mpz_init_set_si(value, x);
      }
   // @}


  ...........................

   /*! \name Stream I/O */
   friend std::ostream& operator<<(std::ostream& sout, const gmp_bigint& x){
	   sout << x.value;
	   return sout;
   }

   friend std::istream& operator>>(std::istream& sin, gmp_bigint& x)
      {
	   sin >> x.value;
	   return sin;
      }
   // @}
   };

} 

#endif

#endif
First of all, if you're using GNU MP in a C++ program, use mpz_class, not mpz_t.

Second, there aren't any stream insertion/extraction operators for mpz_t or mpz_class (which is what your error message is saying).

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <gmpxx.h>

class gmp_bigint
{
   mpz_class value;
 public:
   explicit gmp_bigint(signed long int x = 0) : value(x) { }
   friend std::ostream& operator<<(std::ostream& sout, const gmp_bigint& x){
           return sout << x.value.get_str(10);
   }
   friend std::istream& operator>>(std::istream& sin, gmp_bigint& x)
   {
       std::string val;
       sin >> val;
       x.value = mpz_class(val);
       return sin;
   }
};
int main()
{
    gmp_bigint test1(-42);
    std::cout << test1 << '\n';
    std::cin >> test1;
    std::cout << test1 << '\n';
}

tested with gcc 4.6.2/gmp-5.0.2 on linux
Last edited on
fixed it turns out it was missing the gmpxx library a the linking stage. Thank you for your help.
Topic archived. No new replies allowed.