【发布时间】:2019-03-25 03:52:26
【问题描述】:
#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 失败
注意:在此处请求的函数模板特化 'std::__1::__gcd' 的实例化 ans = __gcd(ans, a[i]);
我正在使用命令 g++ -std=c++17,该命令适用于除此之外的所有程序。
此代码在使用 g++ 5.4.0 的 code.hackerearth.com 在线编译器上正常工作
编辑:删除 bits/stdc++.h 标头并仅包含必需的标头。
删除后也出现同样的问题。
SAME 代码在在线 IDE 中运行正常。一个这样的ide链接是ONLINE IDE
使用他们的 c++ 编译器和函数 __gcd(a, b) 不会出现任何错误,但是当我在同一 ide 中将其更改为 gcd(a, b) 时,确实会出现找不到该函数定义的错误.
当我在本地机器上运行相同的代码时,一切都以相反的方式发生。 __gcd(a, b) 不起作用,而 gcd(a, b) 起作用。
【问题讨论】:
-
__gcd 是私有的,不要使用它...错误信息中有什么不清楚的地方?它仅适用于无符号类型。
-
您使用的是哪个编译器? g++ 还是 clang++?
-
@vivek:这很不幸。仍然没有借口使用实现私有函数
-
@vivek:实现私有部分指的是 __gcd。双下划线表示实现内部使用的非标准函数。标题是另一个问题