473,396 Members | 1,724 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,396 software developers and data experts.

How do I define a function in a class template?


template <class T>
class Monkey {

int Eat();
};

int Monkey::Eat()
{
return 5;
}
This won't compile. In what way have I defined the function wrongly? Are
you supposed to write them like this?:

template <class T>
class Monkey {

int Eat();
};

template <class T>
int Monkey<T>::Eat()
{
return 5;
}
It seems a bit pedantic to me that you have to that...

-Tomás
Feb 17 '06 #1
3 1150
It seems a bit pedantic to me that you have to that...

Opps I read over my post there and it sounded like I was being sarcastic.

Really, I want to know how to do this properly.

I'll have to get Bjarne's book out of the library again... I used to know
all this stuff about a year ago!

Any help appreciated.

-Tomás
Feb 17 '06 #2
Tomás wrote:
It seems a bit pedantic to me that you have to that...


Opps I read over my post there and it sounded like I was being sarcastic.

Really, I want to know how to do this properly.

I'll have to get Bjarne's book out of the library again... I used to know
all this stuff about a year ago!


You did it right. Your 'Eat' member is _private_ in your class template
'Monkey', so it cannot be used by anybody.

V
--
Please remove capital As from my address when replying by mail
Feb 17 '06 #3
"Tomás" writes:
It seems a bit pedantic to me that you have to that...

Opps I read over my post there and it sounded like I was being sarcastic.

Really, I want to know how to do this properly.

I'll have to get Bjarne's book out of the library again... I used to know
all this stuff about a year ago!

Any help appreciated.


Help doing what? You have a compiler, do you not? Why not try the second
way? If there are problems fix them.. Try adding public:. It not only
looks right, it works for me. What more do you want?
Feb 17 '06 #4

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
4
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC...
14
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function,...
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
3
by: Jason Heyes | last post by:
How can I define the member function of this template class using VC++ 6.0? The only way I got it to work was to define the member function inline. template <typename T> class Seq { public:...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
7
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am running into with such a question when I tried to declare and define a friend template function in a template class, here is the code snippet: #include <iostream> using...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
2
by: Joe Hesse | last post by:
Hi, I have a template class and I want to define an operator << as a friend function. For each instantiation of the class I want a corresponding instantiation of operator <<. The following...
3
by: laikon | last post by:
this question is about how to define friend functions for a class template. the following is an example. template <typename T> class Array { private: T* parr; int sz;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.