在vc下可以编译通过的C++代码,可是拿到gcc下去编就失败

waterflier 2005-08-26 10:23:41
代码其实很简单
#include <list>
using namespace std;
template<typename T>
class mylist
{
public:
add(T* param)
{
m_list.pop_back(param);
}
protected:
list<T*> m_llist;
};

class test
{
public:
void foo() {}
};

void main()
{
mylist<test> ml;
ml.add(new test());
}


在VC下可正常通过,但是在gcc下被告知list接受的不能接受T*的模板参数?

到底怎么回事啊。。。是不是我哪里没有做好配置,还是gcc本身不支持如此简单的 C++特性?
...全文
445 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
bootblack 2005-08-26
  • 打赏
  • 举报
回复
list<T*> iterator iter; // ????

大哥问清楚一点行不?把代码copy上来难道更困难? 太长那就免了
waterflier 2005-08-26
  • 打赏
  • 举报
回复
我不是copy实际的代码,随手写了个例子反映问题,拼写错误在所难免阿。问题的核心是:
有一个class
template <typename T>
{
public:
void foo()
{
list<T*> iterator iter;
iter = m_list.begin();
}
list<T*> m_list

};

这个list的模板参数无法接受T*而只能接受T(通过模板参数T构建的新类型T*)
但是在VC下可以支持这个特性.

gcc的编译提示是 iterator is parsed as a non-type

这个是我要问的问题核心所在。

不好意思。发贴的时候动作太快了。
brianlu 2005-08-26
  • 打赏
  • 举报
回复
还是要严谨一些好
brianlu 2005-08-26
  • 打赏
  • 举报
回复
void add(T* param)
{
m_list.pop_back(param);
}
返回类型还是要加的。
foochow 2005-08-26
  • 打赏
  • 举报
回复
void add(T* param)
这样试试...
TonnyQ 2005-08-26
  • 打赏
  • 举报
回复
应该是少了少了返回类型吧,加上去试试,!
ttuurr 2005-08-26
  • 打赏
  • 举报
回复
"list接受的不能接受"??
什么话??
healer_kx 2005-08-26
  • 打赏
  • 举报
回复
我记得C++标准是不写按照返回int处理吧?
junguo 2005-08-26
  • 打赏
  • 举报
回复
#include <list>
using namespace std;
template<typename T>
class mylist
{
public:
void add(T* param) //你的程序少了返回类型,是gcc对c++标准的支持更严格,和T*没有关系!
{
m_llist.push_back(param);
}
protected:
list<T*> m_llist;
};

class test
{
public:
void foo() {}
};

int main()
{
mylist<test> ml;

ml.add(new test());
return 0;
}
oyd 2005-08-26
  • 打赏
  • 举报
回复
没有说过是绝对必须的。原话如下:
A name used in a template declaration or definition and that is dependent on a templateparameter
is
assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified
by the keyword typename.
snowbirdfly 2005-08-26
  • 打赏
  • 举报
回复
顶一下~~~
北极猩猩 2005-08-26
  • 打赏
  • 举报
回复
按照C++标准来说typename list<T*>::iterator ite;中的typename是绝对必需的。
按照C++标准list<T*>::iterator会被当成是list<T*>的一个静态成员,而不是成员类型。

gcc在此处的行为才是符合C++标准的,而VC与标准不同。

foochow 2005-08-26
  • 打赏
  • 举报
回复
如果一个名称被使用在模板声明或定义中并且依赖于模板参数,则这个名称不被认为是一个类型的名称,除非名称查找到一个合适的类型名称,或者这个名称用关键字typename修饰..
bugebear3 2005-08-26
  • 打赏
  • 举报
回复
奇怪,我在VC6和VC7下编译都通不过.
waterflier 2005-08-26
  • 打赏
  • 举报
回复
我现在已经找到了答案

list<T*>::iterator ite
改成
typename list<T*>::iterator ite
就可以了.
其实这段代码是完全符合C++标准的.在vc下可以正常编译通过,估计这里的问题应该是gcc做模板具现的时机与在编译期确定某个型别是有效型别的时机产生了冲突导致.

希望能有能提供不需要修改源代码的解决方法.
waterflier 2005-08-26
  • 打赏
  • 举报
回复
list<T*>::iterator iter;

sorry.代码在linux下,不好copy阿。

64,651

社区成员

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

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