奇怪的模板问题。 [英] strange template problem.

查看:72
本文介绍了奇怪的模板问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi.all

请按以下方式编码:

A级

{

public:

模板< class Tcreate(){return new T;}

};

int main()

{

A a;

int * n = a.create< int>();

删除n;

返回0;

};

编译失败,vc6但在linux下使用g ++成功。

Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.

推荐答案



Gary li写道:

Gary li wrote:

Hi.all

看起来编码如下:

A级

{

公开:

模板< class Tcreate(){return new T;}

};

int main()

{

A a;

int * n = a.create< int>();

删除n;

返回0;

};

使用vc6编译失败但在linux下使用g ++成功。
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.



嗨Gary,


您是否声明了using namespace std;?否则编译器可能不知道
在哪里找不到模板类。在G ++中,这可能与微软的编译器有所不同。这可以解释为什么

结果使用不同的编译器是不同的。


希望这会有所帮助,

Hi Gary,

Did you declare "using namespace std;"? Otherwise the compiler might
not know where to find the template class. This might be handled
differently in G++ than in Microsoft''s compiler. This would explain why
the results are different using different compilers.

Hope this helps,


" Gary li" < li ************* @ sina.com在留言中写道

新闻:11 ***************** ****@m79g2000cwm.googlegro ups.com
"Gary li" <li*************@sina.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com

Hi.all

请按以下方式编码:

class A

{

public:

template< class Tcreate(){return new T;}

};

int main()

{

A a;

int * n = a.create< ; int>();

删除n;

返回0;

};

编译失败vc6但在linux下用g ++成功了。
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.



你还没有为你的create()函数指定一个返回类型,所以它应该在任何地方都不能编译.b / b >

也许上面的代码不是你的实际代码。总是将你的帖子复制并粘贴到

,永远不要重新输入,因为重新输入有很高的错误风险。


添加返回类型的T *并删除假的;在main()之后将得到

在任何符合标准的编译器上编译的代码。它不会在

VC ++ 6上编译,因为VC ++ 6在

模板领域存在严重的合规性问题。


-

John Carson


You haven''t specified a return type for your create() function, so it should
not compile anywhere.

Perhaps the above code is not your actual code. Always copy and paste into
your post, never retype since retyping has a high risk of error.

Adding a return type of T* and removing the spurious ; after main() will get
the code to compile on any Standard compliant compiler. It won''t compile on
VC++ 6 because VC++ 6 has major compliance problems in the area of
templates.

--
John Carson



如果我将模板功能移出课堂,它可以编译好。

模板< class TT * create(){return new T;}

int main()

{

int * n =创建< int>();

printf("%d \ n",* n);

删除n;

返回0;

}


Gary li写道:
If I move template function out of class it can compile ok.
template<class TT* create(){return new T;}
int main()
{
int * n = create<int>();
printf("%d\n", *n);
delete n;
return 0;
}

Gary li wrote:

Hi.all

请看如下编码:

A级

{

公开:

template< class Tcreate(){return new T;}

};

int main()

{

a a;

int * n = a.create< int>();

删除n;

返回0;

};

编译失败,vc6但成功使用g ++ Linux操作系统。
Hi.all
look coding as below please:
class A
{
public:
template<class Tcreate(){return new T;}
};
int main()
{
A a;
int *n = a.create<int>();
delete n;
return 0;
};
compiling is fail with vc6 but successful with g++ under linux.


这篇关于奇怪的模板问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆