2

I am trying to write a base conversion program that will work for numbers beyond the limits of C++'s long long integers. I am trying to use boost multiprecision libraries but have not made it very far.

I have tried to compile the following:

#include <boost/multiprecision/gmp.hpp>
#include <iostream>
#include <string>
#include <stdio.h>

namespace boost{ namespace multiprecision{

class gmp_int;

typedef number<gmp_int >         mpz_int;

}} // namespaces
 using namespace boost::multiprecision;
int main(int argc, const char * argv[]) {
mpz_int seed = 18446744073709551615;

 std::cout << seed;
  return 0;
}

and received the following errors:

    main.cpp:17:20: error: reference to 'gmp_int' is ambiguous
typedef number<gmp_int >         mpz_int;
               ^


main.cpp:15:11: note: candidate found by name lookup is
  'boost::multiprecision::gmp_int'
class gmp_int;
      ^

/usr/local/boost_1_57_0/boost/multiprecision/gmp.hpp:2157:40: note:      candidate
  found by name lookup is 'boost::multiprecision::gmp_int'
using boost::multiprecision::backends::gmp_int;
                                   ^

main.cpp:17:20: error: reference to 'gmp_int' is ambiguous
typedef number<gmp_int >         mpz_int;
               ^

main.cpp:15:11: note: candidate found by name lookup is
  'boost::multiprecision::gmp_int'
class gmp_int;
      ^

 /usr/local/boost_1_57_0/boost/multiprecision/gmp.hpp:2157:40: note: candidate
  found by name lookup is 'boost::multiprecision::gmp_int'
using boost::multiprecision::backends::gmp_int;
                                   ^

main.cpp:17:38: error: typedef redefinition with different types ('number<class
  boost::multiprecision::gmp_int>' vs 'number<struct
  boost::multiprecision::backends::gmp_int>')
typedef number<gmp_int >         mpz_int;
                                 ^

/usr/local/boost_1_57_0/boost/multiprecision/gmp.hpp:2214:34: note: previous
  definition is here
typedef number<gmp_int >         mpz_int;
                             ^

main.cpp:22:2: error: reference to 'gmp_int' is ambiguous
 gmp_int seed = 18446744073709551615;
 ^

/usr/local/boost_1_57_0/boost/multiprecision/gmp.hpp:2157:40: note: candidate
  found by name lookup is 'boost::multiprecision::gmp_int'
using boost::multiprecision::backends::gmp_int;
                                   ^

main.cpp:15:11: note: candidate found by name lookup is
    'boost::multiprecision::gmp_int'
class gmp_int;
      ^

main.cpp:32:18: error: use of undeclared identifier 'seed' std::cout << seed; ^ 5 errors generated.

I know I must be making some pretty basic mistakes but I haven't had any success in fixing them. any help would be appreciated.

I got a simple program to work using #include <boost/lambda/lambda.hpp>, so I am fairly certain that boost is installed correctly.

sehe
  • 374,641
  • 47
  • 450
  • 633
Jonathan Basile
  • 649
  • 1
  • 10
  • 20
  • also, use `18446744073709551615ull` or initialize from string `mpz_int seed("18446744073709551615");` – sehe Apr 08 '15 at 21:05

2 Answers2

2

You are declaring a new type class gmp_int;. I cannot fathom any reason why you would want to do so.

However, this means that the next line

typedef number<gmp_int >         mpz_int;

now is unable to decide whether you mean your new type ::gmp_int or Boosts version ::boost::multiprecision::gmp_int.

Your C++ compiler is attempting to tell you that it cannot decide which gmp_int is meant with its message "reference to 'gmp_int' is ambiguous".

danielschemmel
  • 10,885
  • 1
  • 36
  • 58
  • O I see - I was a bit confused by the documentation - if i remove the class definition i still get the following errors: main.cpp:17:17: warning: integer constant is larger than the largest signed integer type gmp_int seed = 18446744073709551615; ^ main.cpp:17:10: error: no viable conversion from 'unsigned long long' to 'boost::multiprecision::backends::gmp_int' gmp_int seed = 18446744073709551615; – Jonathan Basile Apr 08 '15 at 21:09
  • @JonathanBasile I already addressed that in my comment. Also, see my answer – sehe Apr 08 '15 at 21:10
2

It looks like you copied the wrong bits from the documentation: http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/gmp_int.html

What's shown is a reference summary, not a usage synopsis. The types shown already exist.

A clear case of less is more:

Live On Coliru

#include <boost/multiprecision/mpfr.hpp>
#include <iostream>

int main() {
    boost::multiprecision::mpz_int seed = 18446744073709551615ull;
    std::cout << seed;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • This is very helpful - however, I am still getting the following errors: Undefined symbols for architecture x86_64: "___gmp_get_memory_functions", referenced from: boost::multiprecision::backends::gmp_int::str(long, unsigned int) const in main-8ba45a.o "___gmpz_clear", referenced from: boost::multiprecision::backends::gmp_int::~gmp_int() in main-8ba45a.o "___gmpz_get_str", referenced from: boost::multiprecision::backends::gmp_int::str(long, unsigned int) const in main-8ba45a.o – Jonathan Basile Apr 08 '15 at 21:13
  • "___gmpz_init", referenced from: boost::multiprecision::backends::gmp_int::operator=(unsigned long) in main-8ba45a.o boost::multiprecision::backends::gmp_int::gmp_int() in main-8ba45a.o "___gmpz_set_ui", referenced from: boost::multiprecision::backends::gmp_int::operator=(unsigned long) in main-8ba45a.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – Jonathan Basile Apr 08 '15 at 21:13
  • how am i spamming? im just trying to put the errors messages here because i assume they're necessary to isolate the problem – Jonathan Basile Apr 08 '15 at 21:15
  • I cannot possibly make the sample more interactive. Follow the **[Live On Coliru](http://coliru.stacked-crooked.com/a/2b1449285dcbda71)** link and see for yourself. [Here's a clang variant](http://coliru.stacked-crooked.com/a/3888d893a658598c). Hope that helps... – sehe Apr 08 '15 at 21:16
  • The problem I'm having is that I havent been able to reproduce your results. I see that the code works on coliru but not on my machine. if you arent willing to help further that's fine but i have to let people know that im still having a problem if someone else is going to. - i notice that you're using mpfr.hpp - that's not a library i have. And i know i havent been able to isolate the problem - that's why im asking for help – Jonathan Basile Apr 08 '15 at 21:22
  • @JonathanBasile fixed that for you **[gmp only, clang](http://coliru.stacked-crooked.com/a/22fc10f93d9d6611)**. It's a force of habit to include mpfr I guess :/ – sehe Apr 08 '15 at 21:24
  • When I try to use the gmp library I get the "Undefined symbols for architecture x86_64:" message - does that mean I have 32-bit boost? – Jonathan Basile Apr 08 '15 at 21:29
  • 3
    @JonathanBasile I really don't know. It could be you're not linking `-lgmp` (in the right order!!!) or indeed your `gmp` is not 64 bit. (Boost is header only here, so that doesn't matter). If you don't work it out, post a new question about the linker error and tag it [tag:boost] [tag:c++] [tag:osx] [tag:clang++] – sehe Apr 08 '15 at 21:32
  • I didn't flag you - someone else did - so evidently I'm not the only person who finds your attitude unnecessary - and the point im making in comments is that beyond the three issues you addressed in your answer (which was very helpful) i am now having another issue which has emerged since making the changes you suggested. again, if you dont want to help with this further problem, im just mentioning it in the hopes that someone else will. – Jonathan Basile Apr 08 '15 at 21:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74761/discussion-between-sehe-and-jonathan-basile). – sehe Apr 08 '15 at 21:39