求助:STL的问题

yc_8301 2006-08-14 01:01:25

#include "Test.h"

void Test::PrintTest(){
cout << "Print Test!\n";
return;
}

bool iteratorA::less_than( int v1, int v2){
return v1 < v2 ? true : false;
}
bool iteratorA::greater_than(int v1, int v2){
return v2 > v1 ? true : false;
}

vector<int> iteratorA::filter( const vector<int> &vec,
int filter_value,
bool (*pred)(int,int)){
vector<int> nvec;
for(int ix = 0; ix < vec.size(); ++ix){
if( pred(vec[ix], filter_value) ){
nvec.push_back(vec[ix]);
}
}
return nvec;
}

template <typename ArrayType>
void displayArrayA(const ArrayType &array){
for(int i = 0; i < array.size(); ++i){
cout << "array[" << i << "]: " << array[i] << endl;
}
return;
}

int main(){
cout << "Hello World!\n";

/*
Test* test = new Test;
try{
test->PrintTest();
}catch(const std::exception &error){
cerr << error.what() << endl;
delete test;
}
delete test;
*/
auto_ptr<Test> test(new Test);
test->PrintTest();

//vector<int> big_vec;
int iarray[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
vector<int> big_vec(iarray,iarray + 15);
int value = 10;
iteratorA iteratora;
vector<int> lt_10 = iteratora.filter(big_vec, value, iteratorA::less_than);
displayArrayA(lt_10);
return (0);
}
我是一个新手,关于上面的代码,我在VC6中能够编译成功,但是,在Eclipse + MinGW却发生了下面的错误。请大侠们
指点,谢谢先。。。!!!


**** Incremental build of configuration Debug for project Hello C++ ****

make -k all
'Building file: ../hello.cpp'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -ohello.o ../hello.cpp
../hello.cpp: In member function `std::vector<int, std::allocator<int> > iteratorA::filter(const std::vector<int, std::allocator<int> >&, int, bool (*)(int, int))':
../hello.cpp:22: warning: comparison between signed and unsigned integer expressions
../hello.cpp: In function `void displayArrayA(const ArrayType&) [with ArrayType = std::vector<int, std::allocator<int> >]':
../hello.cpp:60: instantiated from here
../hello.cpp:32: warning: comparison between signed and unsigned integer expressions
'Finished building: ../hello.cpp'
' '
'Building target: Hello.exe'
g++ -o Hello.exe hello.o
'Finished building: Hello.exe'
Build complete for project Hello C++

说是这条语句有问题:displayArrayA(lt_10);
谢谢大侠帮帮忙吧。呜呜呜。。。

...全文
287 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yc_8301 2006-08-14
  • 打赏
  • 举报
回复
"贴子回复次数大于跟给分次数"
怎么不让我 给你们分呀.晕死了.

呜呜.
sinall 2006-08-14
  • 打赏
  • 举报
回复
……无语中……呵呵……哎……
healer_kx 2006-08-14
  • 打赏
  • 举报
回复
平分就可以了,多给我点顺便~
yc_8301 2006-08-14
  • 打赏
  • 举报
回复
OK了.
我直接把Warning该掉就编译OK了,以前用VC时,总是无视警告信息.看来换编译器还真的要注意!!!呵呵.
谢谢各位大侠.但是,我怎么为你分呀.!!!!
sinall 2006-08-14
  • 打赏
  • 举报
回复
报错了吗?没看到报错信息啊?
warning: comparison between signed and unsigned integer expressions
警告:在有符号整形和无符号整形比较的表达式
我啃 2006-08-14
  • 打赏
  • 举报
回复
编译器毕竟是机器,识别和退到能力很差,最好比较明显地显式告诉编译器
yc_8301 2006-08-14
  • 打赏
  • 举报
回复
谢谢几位大侠。我试一试!
healer_kx 2006-08-14
  • 打赏
  • 举报
回复
这个地方不是一定要加的,只是那个编译器的推导能力差。。。
Dong 2006-08-14
  • 打赏
  • 举报
回复
displayArrayA(lt_10);
-----------------------
换为:
displayArrayA<vector<int> >(lt_10);
是摸板函数不是一般的函数


而且
displayArrayA函数体的实现最后使用迭代器来实现,size()也一定可以返回元素的大小,好象是返回分配的空间大小了。有可能没有填满
netxuning 2006-08-14
  • 打赏
  • 举报
回复
帮顶
healer_kx 2006-08-14
  • 打赏
  • 举报
回复
注意> >之间的空格。
chenhu_doc 2006-08-14
  • 打赏
  • 举报
回复
这里有几个warning看的还是很清楚的,
vec.size() 返回的是vector<int>::size_type 类型的,属于unsigned类型,
所以把他赋给int型,就造成了两个warnings!

对于其他两个错误,可能就是对vector操作上有点不标准。。
healer_kx 2006-08-14
  • 打赏
  • 举报
回复
在下面的代码里显式地制导实例化吧,这样写试一试。

displayArrayA<vector<int> >(lt_10);
chenhu_doc 2006-08-14
  • 打赏
  • 举报
回复
把test.h贴出来吧!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧