我第一次使用GMP大整数...下面的简单测试无法编译,因为没有找到
get_ui
成员函数。 GMP手册中提到了get_ui
功能。
#include <iostream>
#include <gmpxx.h>
using std::cout;
using std::endl;
int main()
{
mpz_class a(1);
for (auto n = 0U; n < 100; ++n) a *= 10;
cout << a << endl;
auto b = a % 10;
cout << b << endl;
auto c = b.get_ui();
cout << c << endl;
}
编译器输出:
tc0001.cpp:27:14: error: ‘class __gmp_expr<__mpz_struct [1], __gmp_binary_expr<__gmp_expr<__mpz_struct [1], __mpz_struct [1]>, long int, __gmp_binary_modulus> >’ has no member named ‘get_ui’
27 | auto c = b.get_ui();
| ^~~~~~
系统环境:
这里有什么问题?
问题是,因为你使用
auto
来声明 b
,所以类型不是 mpz_class,而是用于优化计算的内部类型。要调用任何 mpz_class 方法,您需要将其转换为 mpz_class。
最简单的解决方法是将 b 声明为 mpz_class 而不是 auto:
mpz_class b = a % 10;
或者您可以显式转换为 mpz_class:
auto c = mpz_class(b).get_ui();