首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >STL错误_without_代码中的任何STL

STL错误_without_代码中的任何STL
EN

Stack Overflow用户
提问于 2012-06-05 20:01:56
回答 2查看 294关注 0票数 2

我用gcc 3.4.4对赛格温。在下面的代码中,我得到了这个相当令人费解的STL错误消息,它根本不使用STL:

代码语言:javascript
复制
#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编译时

我收到以下错误消息:

代码语言:javascript
复制
/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,所以这是相当奇怪的。此外,如果删除行,则错误将消失。

代码语言:javascript
复制
using namespace std;

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

另一方面,如果我将find设为2-参数函数,则错误也会消失(这是相当令人惊讶的),如下所示:

代码语言:javascript
复制
#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.

  • Remove的定义中删除const关键字,这是函数find.

的第三个参数

我想不出任何逻辑上的理由,为什么这样的错误应该发生在一开始,以及为什么它应该被删除-我使用上述任何一个似乎完全无关的步骤。这是一个记录在案的g++错误吗?我试图搜索它,但老实说,我不知道该搜索什么,而我尝试过的几个关键字("STL错误而不使用STL“)没有出现任何内容。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-05 20:05:45

您只是发生了冲突,因为在执行std::find时,您无意地将using namespace std; (其中包含3个参数)拉到全局命名空间中。不管出于什么原因,您的<iostream>#include-ing <algorithm>,或者它的内部实现的一部分(具体来说,是bits/stl_algo.h)。

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

票数 3
EN

Stack Overflow用户

发布于 2012-06-05 20:05:23

您将编译器与标准库(std:: find )中的find版本混为一谈,后者有3个参数,但没有包含参数。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10904103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档