STL 错误_没有_代码中的任何 STL

STL error _without_ any STL in code

本文关键字:STL 任何 没有 错误 代码      更新时间:2023-10-16

我在cygwin上使用gcc 3.4.4。 我在下面的代码中收到这个相当令人困惑的 STL 错误消息,它根本不使用 STL:

#include <iostream>

using namespace std;
const int N = 100;
bool s[N + 1];
bool p[N + 1];
bool t[N + 1];
void find(const bool a[], bool b[], bool c[]){
  return;
}

int main(){
  find(s, p, t);
  return 0;
}

当我编译时 G++ stack.cc

我收到以下错误消息:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = bool*, _Tp = bool[101]]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:314:   instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = bool*, _Tp = bool[101]]'
stack.cc:18:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:207: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:211: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:215: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:219: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:227: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:231: error: ISO C++ forbids comparison between pointer and integer
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:235: error: ISO C++ forbids comparison between pointer and integer

如您所见,代码根本不使用任何 STL,所以这很奇怪。此外,如果我删除该行,错误就会消失

using namespace std;

这暗示了一些命名空间冲突。 如果我从函数的定义中删除 const 关键字,它也消失find.

另一方面,如果我按如下方式find 2 参数函数,错误消失了(这相当令人惊讶):

#include <iostream>

using namespace std;
const int N = 100;
bool s[N + 1];
bool p[N + 1];
bool t[N + 1];
void find(const bool a[], bool b[]){
  return;
}

int main(){
  find(s, p);
  return 0;
}

我无法想象 find 可以是两个参数函数而不是三个参数函数的原因是什么。

因此,以下是消除错误的三种方法的简要摘要:

  1. 删除using namespace std;行。

  2. find 的定义中删除 const 关键字。

  3. 删除函数的第三个参数find

我想不出任何合乎逻辑的原因,为什么首先应该发生这样的错误,以及为什么应该删除它,我使用上述任何看似完全不相关的步骤。 这是一个记录在案的 g++ 错误吗?我尝试搜索它,但老实说,我不知道要搜索什么,我尝试的几个关键字("没有使用 STL 的 STL 错误")没有出现任何东西。

您只是发生了冲突,因为当您执行 using namespace std; 时,您无意中将std::find(需要 3 个参数)拉入全局命名空间。 无论出于何种原因,您的<iostream> #include <algorithm> ,或其内部实现的一部分(特别是bits/stl_algo.h)。

我无法解释为什么删除const会让它消失;也许它会影响编译器解析重载的顺序。

您将编译器与标准库 (std::find) 中的 find 版本混淆了,该版本具有 3 个参数,但不是您拥有的参数。

如果您的代码位于其自己的命名空间中,则可以避免此问题。 或者重命名查找方法或已记录的解决方案。