| View previous topic :: View next topic |
| Author |
Message |
Gary li Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: strange template problem. |
|
|
Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){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. |
|
| Back to top |
|
 |
Gary li Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: strange template problem. |
|
|
If I move template function out of class it can compile ok.
template<class T> T* create(){return new T;}
int main()
{
int * n = create<int>();
printf("%d\n", *n);
delete n;
return 0;
}
Gary li wrote:
| Quote: | Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){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. |
|
|
| Back to top |
|
 |
Rickfjord Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: strange template problem. |
|
|
Gary li wrote:
| Quote: | Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){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.
|
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, |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Wed Aug 16, 2006 9:10 am Post subject: Re: strange template problem. |
|
|
"Gary li" <liguoqiang_0121 (AT) sina (DOT) com> wrote in message
news:1155706677.685855.13510 (AT) m79g2000cwm (DOT) googlegroups.com
| Quote: | Hi.all
look coding as below please:
class A
{
public:
template<class T> create(){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.
|
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 |
|
| Back to top |
|
 |
|