C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange template problem.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary li
Guest





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: strange template problem. Reply with 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
Gary li
Guest





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: strange template problem. Reply with quote



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





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: strange template problem. Reply with quote



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





PostPosted: Wed Aug 16, 2006 9:10 am    Post subject: Re: strange template problem. Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.