function template compilation error.

A

Aman

Hi,
I'm using the g++ compiler to compile this on SunOS 5.8.
I get compilation errors when I use A* as return type but none when I
used void .
why is this ?
regards,
Aman.
----------errors -------------------

:6: syntax error before `*'
:20: syntax error before `*'
:20: `T' was not declared in this scope
:20: template argument 1 is invalid
:20: `T' was not declared in this scope
:20: parse error before `)'
:21: ANSI C++ forbids declaration `function' with no type

----------- Code --------------------

#include <iostream>
using namespace std ;
template <typename T>
class test {
public :
A* function(T) ; // using void as return type compiles fine .
private :
struct A{
T somedata ;
int i ;
} ;

};
template <typename T>
A* test<T> :: function(T in) // using void as return type works fine.
{
}
 
J

Jonathan Mcdougall

Hi,
I'm using the g++ compiler to compile this on SunOS 5.8.
I get compilation errors when I use A* as return type but none when I
used void .
why is this ?
regards,
Aman.
----------errors -------------------

:6: syntax error before `*'
:20: syntax error before `*'
:20: `T' was not declared in this scope
:20: template argument 1 is invalid
:20: `T' was not declared in this scope
:20: parse error before `)'
:21: ANSI C++ forbids declaration `function' with no type

----------- Code --------------------

#include <iostream>
using namespace std ;
template <typename T>
class test {
public :
A* function(T) ; // using void as return type compiles fine .
private :
struct A{
T somedata ;
int i ;
} ;

};

Try to put the struct definition before it is used in the class.

template <typename T>
A* test<T> :: function(T in) // using void as return type works fine.

Should be

template <typename T>
test<T>::A* test<T> :: function(T in)


Jonathan
 
A

Aman

This works fine now , thank you very much .

what's the general rule on using qualification for the types ?
when ever a struct etc uses the typename T , we need qualification with
:: ?

regards,
Aman.
 
J

Jonathan Mcdougall

This works fine now , thank you very much .

Acutally, it shouldn't, I missed something :

template <typename T>
test<T>::A* test<T> :: function(T in)

should be

template <typename T>
what's the general rule on using qualification for the types ?
when ever a struct etc uses the typename T , we need qualification with
:: ?

In your example

template <typename T>
class test
{
public :
A* function(T);

private :
struct A
{
T somedata ;
int i ;
} ;
};

'A' is an inner class of test, so it must be qualified as 'test::A' and
since 'test' is a template, the syntax is 'test<T>::A', where T could
be anything else.

Furthermore, 'A' is a dependent name of the template so it must be preceeded
by the keywork 'typename' to denote that 'A' is a type. Note that is only
a summary of the rules, you could get C++ Templates by Josuttis and
Vandevoorde (very recommended) to have more informations.


Jonathan
 
A

Aman

hmm..works fine with and without the "typename" !!

Thanks a lot for the summary .
regards,
Aman.
 
J

Jonathan Mcdougall

Not all compilers complain (yet) about the missing "typename".

Which is why I missed it.
gotcha !
guess it's a good idea to use it anyway .

Of course, since your code will eventually break one day and you
won't have the least idea why.


Jonathan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top