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 

typedef & template

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





PostPosted: Sun Jun 18, 2006 9:10 am    Post subject: typedef & template Reply with quote



Hi!

This code is ok:

template<class T>
struct Container
{
T x;
typedef int size_type;
size_type size() { return sizeof T; }
};

Why the compiler (VC7) does not compile the following code?

template<class T>
struct Container
{
T x;
typedef int size_type;
size_type size() const;
};

template<class T>
Container<T>::size_type Container<T>::size() { return sizeof T; }

---

Thanks

Dmytro
Back to top
Jakob Bieling
Guest





PostPosted: Sun Jun 18, 2006 9:10 am    Post subject: Re: typedef & template Reply with quote



Dmytro <dmytro.bukhantsov (AT) gmail (DOT) com> wrote:

Quote:
Why the compiler (VC7) does not compile the following code?

template<class T
struct Container
{
T x;
typedef int size_type;
size_type size() const;
};

template<class T
Container<T>::size_type Container<T>::size() { return sizeof T; }

Where (which line in the above snippet) and which error do you get?
My guess is you're missing a 'typename' in the last line.

hth
--
jb

(reply address in rot13, unscramble first)
Back to top
Ian Collins
Guest





PostPosted: Sun Jun 18, 2006 9:10 am    Post subject: Re: typedef & template Reply with quote



Dmytro wrote:
Quote:
Hi!

This code is ok:

template<class T
struct Container
{
T x;
typedef int size_type;
size_type size() { return sizeof T; }
};

Why the compiler (VC7) does not compile the following code?

template<class T
struct Container
{
T x;
typedef int size_type;

Why use int for a size_type? Does a negative size make sense?

Quote:
size_type size() const;
};

template<class T
Container<T>::size_type Container<T>::size() { return sizeof T; }

You left out two important things:


typename Container<T>::size_type Container<T>::size() const {
return sizeof T; }

The typename is required to tell the compiler that
Container<T>::size_type is a type and the const because the prototype
function is const.

--
Ian Collins.
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.