为什么__gcd()在macOS mojave中抛出错误? [英] Why __gcd() is throwing error in macOS mojave?

查看:54
本文介绍了为什么__gcd()在macOS mojave中抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
   
    for(int i = 0; i < n; ++i)
        cin >> a[i];

    int ans = a[0];
    for(int i = 1; i < n; ++i)
       ans = __gcd(ans, a[i]);

    cout << ans << endl;

    return 0;
}

它抛出以下错误:

错误:由于要求'!is_signed :: value',因此static_assert失败

error: static_assert failed due to requirement '!is_signed::value'

注意:在此处要求的功能模板特化'std :: __ 1 :: __ gcd'的实例中ans = __gcd(ans,a [i]);

note: in instantiation of function template specialization 'std::__1::__gcd' requested here ans = __gcd(ans, a[i]);

我正在使用命令g ++ -std = c ++ 17,该命令适用于除此程序以外的所有程序.

I am using command g++ -std=c++17 which worked for every program except this one.

此代码在使用g ++ 5.4.0的code.hackerearth.com在线编译器上正常运行

This code is working without error on code.hackerearth.com online compiler which uses g++ 5.4.0

删除了bits/stdc ++.h标头,仅包括必需的标头.

Removed bits/stdc++.h header and included required headers only.

删除后,同样的问题也发生了.

After removing also the same problem is happening.

SAME代码在联机ide中正常运行.一个这样的想法的链接是在线IDE

The SAME code is running properly in online ide. Link of one such ide is ONLINE IDE

使用他们的c ++编译器和函数__gcd(a,b)不会给出任何错误,但是,当我在同一个想法中将其更改为gcd(a,b)时,确实给出了未找到此函数定义的错误

Using their c++ compiler and function __gcd(a, b) doesn't give any error but, when I change it to gcd(a, b) in the same ide, it does give error that this function definition is not found.

当我在本地计算机上运行相同的代码时,一切都以相反的方式发生.__gcd(a,b)不起作用,而gcd(a,b)起作用.

When I run the same code in my local machine, everything happens in just the opposite way. __gcd(a, b) doesn't work while gcd(a, b) works.

推荐答案

不要使用 bit/C ++.h ,它是一个私有标头.

Don't use bit/C++.h, it's a private header.

使用适当的C ++函数: https://en.cppreference.com/w/cpp/numeric/gcd

Use proper C++ functions: https://en.cppreference.com/w/cpp/numeric/gcd

它们支持带符号整数.

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
int n;
cin >> n;
vector<int> a(n);

for(int i = 0; i < n; ++i)
    cin >> a[i];

int ans = a[0];
for(int i = 1; i < n; ++i)
   ans = gcd(ans, a[i]);

cout << ans << endl;

return 0;
}

clang ++ -std = c ++ 17 一起使用.

这篇关于为什么__gcd()在macOS mojave中抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆