【求救】MS VC 能编译,gcc 报错,为何?

keios 2006-03-28 05:34:29
#include <list>

template<class T>
bool my_find(std::list<T *> *pList, const T* val)
{
for (std::list<T *>::iterator it = pList->begin(); it != pList->end(); ++it)
{
if (*it == val)
return true;
}
return false;
}

int main()
{
return 0;
}

---------------------------------------------------------------------
以上代码,使用微软VS 2003 能够顺利编译通过,但是使用 gcc 3.4.5 却报错:
test1.cpp: In function `bool my_find(std::list<T*, std::allocator<T*> >*, const T*)':
test1.cpp:6: error: expected `;' before "it"
test1.cpp:6: error: `it' undeclared (first use this function)
test1.cpp:6: error: (Each undeclared identifier is reported only once for each function it appears in.)

请问如何修改代码使其在 gcc 下不报错?
...全文
167 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
keios 2006-03-28
  • 打赏
  • 举报
回复
我按照村少的方法解决了!
多谢二位!
healer_kx 2006-03-28
  • 打赏
  • 举报
回复
别问我了..我Linux鸟不懂,但是我觉得list等,应该间接地包含了<iterator>

也许村少说得是对的.
通常,typename表示指定模板T内的子类型为类型,而不是static成员或者enum.
但是这里, list<T*>::也许要指定.

但是我如下的程序,在VC7内也是要使用typename关键字的,否则实例化出错.但是你这种情况就不用,所以我不能肯定村少说的.
所以不能肯定任何一个答案.

template<class T>
class MyClass
{
typedef typename T::sub_type sub_type; //详见C++ Templates
}
cunsh 2006-03-28
  • 打赏
  • 举报
回复
for (typename std::list<T *>::iterator it = pList->begin(); it != pList->end(); ++it)
keios 2006-03-28
  • 打赏
  • 举报
回复
to healer_kx(天降甘草的甘草)
不行啊,还是在同样的地方报错

to cunsh(村少)
能否详细讲一下typename std::list<T *>::iterator加到哪里?
fatalerror99 2006-03-28
  • 打赏
  • 举报
回复
在 gcc 中编译要加上 #include <iterator>
cunsh 2006-03-28
  • 打赏
  • 举报
回复
typename std::list<T *>::iterator
healer_kx 2006-03-28
  • 打赏
  • 举报
回复
挺好的一段代码...可怜的GCC.
std::list<T *>::iterator it = pList->begin();
拿到for的前面试一试.

64,651

社区成员

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

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