Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 1st, 2007, 6:43 PM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0 King is an unknown quantity at this point
Template Questions

I just have a few questions about templates. I will just use the following class as an example to use for my questions:
template <class T>
class MyClass
{
public:
	MyClass() {};
	~MyClass() {};
	void MyFunction(const T &);
};

template<class T>
void MyClass<T>::MyFunction(const T &value)
{
	// do something
}
Usually when I write a class I would put the implementation of the functions in a separate .cpp file, but when I do this I get compile errors. But if I put the implementation of the function in the same file as the class definition it works fine, why is that?
My second question is if I have to keep the implementation of my functions in the same file, would there be a point to still define it outside if the class definition like I have shown above, or should I just define it inside the class?
Thanks.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Feb 1st, 2007, 8:01 PM   #2
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Too tired to phrase an intelligent answer to the first but this link seems to do a good job at explaining the details.

On the second question, well for your example it is largely just a matter of taste, but again the mentioned link goes more into the specifics.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Feb 2nd, 2007, 2:44 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
can't give any cool details...BUT i've been told that when you use templates the declarations and definitions need to be in one file.

the reason you find weird shit like this is that C++ is not a ground-up new language. it's a wrapper for C. so add-on crap like templates involves weird shit so they don't have to rewrite the whole compiler.

you just gotta live with it.

good luck!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Feb 3rd, 2007, 7:30 PM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0 King is an unknown quantity at this point
Thanks for the info guys. Is there any difference at all if I define the function inline or outside of the class definition?
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
friends, templates, and other s**t bl00dninja C++ 4 Oct 14th, 2006 1:15 AM
Help: Template Class' copy constructor MicahCarrick C++ 2 Jan 23rd, 2006 5:33 AM
Function Template Question aznluvsmc C++ 6 Oct 7th, 2005 11:37 PM
Template + operator problem Polyphemus_ C++ 3 Sep 30th, 2005 6:43 PM
nontype default template parameters earl C++ 4 Jun 21st, 2005 11:27 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC