我用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可以是两个参数函数,而不是三个参数函数。
因此,这里简要总结了消除错误的三种方法:
从find.
const关键字,这是函数find.的第三个参数
我想不出任何逻辑上的理由,为什么这样的错误应该发生在一开始,以及为什么它应该被删除-我使用上述任何一个似乎完全无关的步骤。这是一个记录在案的g++错误吗?我试图搜索它,但老实说,我不知道该搜索什么,而我尝试过的几个关键字("STL错误而不使用STL“)没有出现任何内容。
发布于 2012-06-05 20:05:45
您只是发生了冲突,因为在执行std::find时,您无意地将using namespace std; (其中包含3个参数)拉到全局命名空间中。不管出于什么原因,您的<iostream>是#include-ing <algorithm>,或者它的内部实现的一部分(具体来说,是bits/stl_algo.h)。
我无法解释为什么删除const会使其消失;也许它会影响编译器解析重载的顺序。
发布于 2012-06-05 20:05:23
您将编译器与标准库(std:: find )中的find版本混为一谈,后者有3个参数,但没有包含参数。
如果您的代码位于自己的命名空间中,则可以避免此问题。或者重命名find方法,或者重命名您已经记录的解决方案。
https://stackoverflow.com/questions/10904103
复制相似问题