typedef'd return type of a template class member function: take 2

P

Pete Becker

Dave said:
The following code won't compile for me, it does not
recognize my typedef'd type as a type:

In general, you should include the exact error message that you got.
template <class T> A<T>::TD A<T>::b () {

Probably need:

template <class T> typename A<T>::TD A<T>::b () {

A<T>::TD is assumed to name a data object unless you tell the compiler
that it's the name of a type.
 
D

Dave

The following code won't compile for me, it does not
recognize my typedef'd type as a type:

template <class T> class A {
public:
typedef int TD;
private:
TD b ();

};

template <class T> A<T>::TD A<T>::b () {

// Stuff.

}

This would work fine if it was not a template class, it's something I
use regularly to keep my class's types clear and consistent without
polluting their containing namespace. Can anyone tell me how I can
accomplish this with a template?

Thanks!
Dave Corby

P.S.
Sorry about the lack of indentation, I'm at work and can't install a
real NNTP client so I'm posting this through Google.
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Dave said:
The following code won't compile for me, it does not
recognize my typedef'd type as a type:

template <class T> class A {
public:
typedef int TD;
private:
TD b ();

};

template <class T> A<T>::TD A<T>::b () {

// Stuff.

}

You need to tell the compiler that TD is the name of a type. Try this:

template <class T> typename A<T>::TD A<T>::b ()
{
// Stuff.

}

(Note 'typename'...)

Ali
 
D

Dave

I tried out what you have both suggested, and now I get the error:

error: no `typename A<T>::TD A<T>::b()' member function declared in
class `A<T>'

Now it looks like all I have to do is get the declaration to match the
definition, but I can't imagine how I can change my declaration and
still have it mean the same thing.

Thanks for all your help so far, and sorry about my posting skills, I
am working on them though :)
 
D

Dave

Oh, so sorry, I found my problem. Your solution fixed it, I just
mistyped a character in the function name and posted again too hastily.
Thank you both again very much!
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Dave said:
I tried out what you have both suggested, and now I get the error:

error: no `typename A<T>::TD A<T>::b()' member function declared in
class `A<T>'

This is the complete code that works with g++ both 3.4.2 and 2.95.3:

template <class T> class A
{
public:
typedef int TD;
public:
TD b ();
};

template <class T> typename A<T>::TD A<T>::b ()
{
// Stuff.
}

int main()
{}

Maybe your compiler is too old?
Now it looks like all I have to do is get the declaration to match the
definition,

This is not needed and wouldn't work. You use the 'typename' keyword
whenever a name may change meaning depending on a template parameter. For
example, it is possible that a specialization of the template can introduce
the same name as an object.

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top