473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can't understand template (Short Code I promise!)


First of all, the following will compile for me without any problems:

class BearerOfType
{
public:

class SpecialType {};
};
template<class T>
class Base
{
public:

typedef typename T::SpecialType SpecialType;

int NormalMemberFunction() const;
};

template<class T>
int Base<T>::NormalMemberFunction() const
{
return 5;
}

int main()
{
Base< BearerOfType > k;
}

The compiler compiles it silently with no errors and no warnings.

Now, if I change the return type of the member function from "int" to
"SpecialType", then I get a weird syntax error. First of all here's the code
(copied verbatim with line numbers):

01: class BearerOfType
02: {
03: public:
04:
05: class SpecialType {};
06: };
07:
08:
09: template<class T>
10: class Base
11: {
12: public:
13: typedef typename T::SpecialType SpecialType;
14:
15: SpecialType NormalMemberFunction() const;
16: };
17:
18: template<class T>
19: SpecialType Base<T>::NormalMemberFunction() const
20: {
21: return SpecialType();
22: }
23:
24: int main()
25: {
26: Base< BearerOfType > k;
27: }

And here's what my compiler gave me:

toopie.cpp:19: syntax error before `<' token
toopie.cpp:19: `T' was not declared in this scope
toopie.cpp:19: template argument 1 is invalid
toopie.cpp:20: warning: ISO C++ forbids declaration of
`NormalMemberFunction'
with no type
toopie.cpp:20: non-member function `int NormalMemberFunction()' cannot have
`
const' method qualifier
toopie.cpp: In function `int NormalMemberFunction()':
toopie.cpp:21: `SpecialType' undeclared (first use this function)
toopie.cpp:21: (Each undeclared identifier is reported only once for each
function it appears in.)
I was sitting there scratching my head thinking what could be wrong... until
I changed the function definition from:

template<class T>
SpecialType Base<T>::NormalMemberFunction() const

to:

template<class T>
Base<T>::SpecialType Base<T>::NormalMemberFunction() const
Now it compiles, but it gives a warning:

toopie.cpp:20: warning: `typename Base<T>::SpecialType' is implicitly a
typename
toopie.cpp:20: warning: implicit typename is deprecated, please see the
documentation for details
Anyone know what's going on here? Any advice appreciated.
-Tomás
Mar 5 '06 #1
3 1677
Tomás wrote:

[working code snipped]

01: class BearerOfType
02: {
03: public:
04:
05: class SpecialType {};
06: };
07:
08:
09: template<class T>
10: class Base
11: {
12: public:
13: typedef typename T::SpecialType SpecialType;
14:
15: SpecialType NormalMemberFunction() const;
16: };
17:
18: template<class T>
19: SpecialType Base<T>::NormalMemberFunction() const
typename Base<T>::SpecialType Base<T>::NormalMemberFunction() const
20: {
21: return SpecialType();
22: }
23:
24: int main()
25: {
26: Base< BearerOfType > k;
27: }

[error messages and incomplete resolution snipped]

Mar 5 '06 #2
typename Base<T>::SpecialType Base<T>::NormalMemberFunction() const

Thanks.

Okay it works; but in my opinion, it's horribly verbose! I have to write
out the long-winded:

typename Base<T>::SpecialType

instead of:

SpecialType

-Tomás
Mar 5 '06 #3

Tomás wrote:
typename Base<T>::SpecialType Base<T>::NormalMemberFunction() const

Thanks.

Okay it works; but in my opinion, it's horribly verbose! I have to write
out the long-winded:

typename Base<T>::SpecialType

instead of:

SpecialType


Well, yeah... you're outside the class definition, so members of the
class must be qualified with the classname. If Base weren't a template,
you'd be able to save yourself the "typename" part, but that's it. If
you define the function inline, you can save yourself the trouble.

Josh

Mar 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SAN CAZIANO | last post by:
can you help me please because I can't understand why the code doesn't seems to function very well: in onkeypress it must verify if insert number or string value in the fiels, but it doesn't...
8
by: Steven T. Hatton | last post by:
There was recently some discussion of storing source code in databases. To some extent that is a rather common practice. CVS, for example, provides a means of storing source code in a database. ...
13
by: Terry Andersen | last post by:
Could anyone please help me understand what is going on in the code below. A short explanation would be nice..... Best Regards Terry struct mystruct { unsigned char testchar; unsigned short...
0
by: CMEDIA_SOUND | last post by:
I have a peculiar problem, I have a tabpage with a label control on it. When i set a background image to the tabpage and drag the label around it has paint issues in that it is slow, granted the...
2
by: saarcarmi | last post by:
Hi Could anyone help me to understand why in the following code sum1 is different from sum2? Basically it's the same calculation done. Keep in mind the x is 28 digits so it's within the range...
4
by: Tomás | last post by:
I can't understand why this won't compile: template <class T> struct Monkey { typedef T Normal; }; template <class T>
3
by: Daniel | last post by:
LoaderLock was detected Message: DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX.Direct3D\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.Direct3D.dll' is attempting managed execution inside OS...
5
gpraghuram
by: gpraghuram | last post by:
HI all, I wrote the following piece of code class A { A(){cout<<"ctor"<<endl; ~A(){cout<<"dtor"<<endl; }; int main()
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.