Блог пользователя Saiyan_1991

Автор Saiyan_1991, история, 4 года назад, По-английски

my g++ version is 9.3.0 but std::lcm() std::gcd() is not working. An error message is showing that "'lcm' was not declared in this scope" or "'gcd' was not declared in this scope". what should I do?

UPD: problem solved...keeping this post for those people who are encountering similar problem.

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

There're no such functions called gcd() or lcm(). (only before C++17,thanks MohamedMagdy for correcting my mistake)

However, there're something called __gcd()

If you want the GCD of a and b, you can write the following code:

int gcd(int a,int b){
    if(b==0)return a;
    else return gcd(b,a%b);
}

And for LCM:

int lcm(int a,int b){
    return a*b/gcd(a,b);
}
»
4 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Try to compile with -std=c++17 option.

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

    i have latest c++17 minGW compiler(before c++20) in my machine. But it still showing this error. How should i use this in my local compiler?

    I can use all the functionality of c++17 except using std::gcd() and std::lcm()

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I compile with -std=c++2a and both gcd and lcm work. You can get C++20 (beta?) compiler from here : http://mingw.org/