Home > OS >  Why am I getting error while using the Boost Library in C ?
Why am I getting error while using the Boost Library in C ?

Time:11-15

I am using the c boost library in one code to get large numbers in output.

My code is:

#include <iostream>
#include<cmath>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;

int main()
{
    int a;
    cout<<"Enter the value for a:";
    cin>>a;
    for(int x =1;x<=a;x  )
    {
        int128_t ans = pow(a,2*x)-pow(a,x) 1;
        cout<<ans<<endl ;
        
    }
    return 0;
}

But after running this, I am getting following error:

prog.cc: In function 'int main()':
prog.cc:14:43: error: conversion from '__gnu_cxx::__promote_2<int, int, double, double>::__type' {aka 'double'} to non-scalar type 'boost::multiprecision::int128_t' {aka 'boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<128, 128, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void> >'} requested
   14 |         int128_t ans = pow(a,2*x)-pow(a,x) 1;
      |                        ~~~~~~~~~~~~~~~~~~~^~

What is the reason for such an error?

CodePudding user response:

The reason is that ans type is not int128_t. After changing ans type to auto compilation works fine:

cat boost_error.cpp 
#include <iostream>
#include<cmath>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;

int main()
{
    int a;
    cout<<"Enter the value for a:";
    cin>>a;
    for(int x =1;x<=a;x  )
    {
        auto ans = pow(a,2*x)-pow(a,x) 1;
        cout<<ans<<endl ;
        
    }
    return 0;
}

g   boost_error.cpp
a.out
Enter the value for a:3
7
73
703

g   --version
g   (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  •  Tags:  
  • c
  • Related