I'm trying to use Visual Studio Code for C++. Usually I do this on my windows pc but I wanted to use my macbook instead so I could sit somewhere else and while following tutorials I've come across an issue where I made a file
#include <iostream>
#include <numeric>
class Fraction{
int m_numerator=0;
int m_denominator=1;
public:
Fraction(int numerator=0, int denominator=1)
: m_numerator(numerator), m_denominator(denominator) {
reduce();
}
void reduce()
{
int gcd = std::gcd(m_numerator, m_denominator);
if (gcd){
m_numerator /= gcd;
m_denominator /= gcd;
}
}
friend Fraction operator*(const Fraction& f1, const Fraction& f2);
friend std::ostream& operator<<(std::ostream& out, const Fraction& f1);
friend std::istream& operator>>(std::istream& out, Fraction& f1);
void print(){
std::cout << m_numerator << '/' << m_denominator << std::endl;
}
};
Fraction operator*(const Fraction& f1, const Fraction& f2){
return Fraction(f1.m_numerator * f2.m_numerator , f1.m_denominator * f2.m_denominator);
}
std::ostream& operator<<(std::ostream& out, const Fraction& f1){
out << f1.m_numerator << "/" << f1.m_denominator;
return out;
}
std::istream& operator>>(std::istream& in, Fraction& f1){
std::cout << "Enter the Numerator: ";
in >> f1.m_numerator;
std::cout << "Enter the Denominator: ";
in >> f1.m_denominator;
return in;
}
int main()
{
Fraction f1;
std::cin >> f1;
Fraction f2;
std::cin >> f2;
std::cout << f1 << " * " << f2 << " = " << f1*f2 << std::endl;
return 0;
}
when I run this same code on the windows pc using code runner it's fine but when I run it on the mac using code runner I get the following:
fraction.cpp:5:20: warning: default member initializer for non-static data member is a C++11 extension [-Wc++11-extensions]
int m_numerator=0;
^
fraction.cpp:6:22: warning: default member initializer for non-static data member is a C++11 extension [-Wc++11-extensions]
int m_denominator=1;
^
fraction.cpp:18:18: error: no member named 'gcd' in namespace 'std'
int gcd = std::gcd(m_numerator, m_denominator);
~~~~~^
2 warnings and 1 error generated.
I have installed the latest version of Homebrew, installed the latest version of the LLVM, installed the Microsoft C/C++ Extension on VS Code and also installed Code runner.
below is what my c_cpp_properties.json file looks like. I would appreciate any help to try and fix this issue so I'm able to run the code on my mac the same way as I can on my windows pc. Thank you
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
This is tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Changed the version of C++ in the json file settings hoping that it would recognise this and allow the program to run.