2

I'm trying to find out the GCD in C++. I'm using __gcd code as mentioned on this website : https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/

Appended is my code. Can someone please guide as to what is wrong

#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;

class Rational {
    private:
    int num;
    int denom;

    public:
    Rational(int a, int b){
        num = a;
        denom = b;
    }

    int add(){
        return num + denom;
    }

    int sub(){
        return num - denom;
    }

    int mul(){
        return num * denom;
    }

    void gcd(){
    cout <<__gcd(num,denom);
    }


    int simplify(){
        int gcd1 = gcd(num,denom);
        return (num/gcd1,denom/gcd1);
    }

};

int main(){
    Rational r(2,6);
    cout<<r.add()<<endl;
    cout<<r.sub()<<endl;
    cout<<r.mul()<<endl;
    cout<<r.gcd()<<endl;
Omer Qureshi
  • 113
  • 13
  • 2
    Try `r.gcd();` instead of `cout << r.gcd() << endl;` – NemPlayer Oct 05 '19 at 11:58
  • 3
    There is no `__gcd()` function in the standard library. Identifiers that begin with two underscores are reserved for the implementation so `__gcd()` is some kind of library-specific internal function. You're code might work with some compilers but it won't compile with Visual Studio. Don't learn C++ from sketchy website articles. – Blastfurnace Oct 05 '19 at 12:40
  • @Omer Qureshi, you can check at https://stackoverflow.com/questions/68772236/what-is-the-difference-between-std-gcd-and-stdgcd?noredirect=1&lq=1, there is a good response to this question. – Roberto C. Rodriguez-Hidalgo Oct 23 '21 at 11:59

0 Answers0