LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-12-2004, 09:52 AM   #1
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
function prototypes in template class' (c++)


hey guys..
i have written a template class which i need to split into a .h header file with the class prototype, and a .cpp file with all the function boddies.

i know how to do this, but i cant get the syntax right when i do it wilth templates..

for example, the class
Code:
class chelloworld {
public:
    bool printMessage(char *message) {
         cout << "hello world:" << message << endl;
         return true;
    }
};
can be split into the format i need like so....
header file (prototype)
Code:
class chelloworld {
public:
    bool printMessage(char*);
};
And .cpp file (function body)
Code:
#include"theHeaderFile.h"
bool chelloworld::printMessage(char *message) {
         cout << "hello world:" << message << endl;
         return true;
}
this is all great.. HOWEVER.. how should i do this for a template class.

for example, how would i do the above for this class

Code:
template<class dataType> class chelloworld {

   dataType doSomthing(dataType DT) {
        dataType COPY;
        COPY.makeCopyof(DT);
        return COPY;
    }

};
HELP !!!

thankyou.
 
Old 12-12-2004, 03:36 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,539

Rep: Reputation: 149Reputation: 149
You simply can't. I don't know any compiler that handles templates divided into .h and .cpp. You need to keep it in one file.
 
Old 12-12-2004, 04:26 PM   #3
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
Quote:
You simply can't.
you have to to compile a shared object !
programs linked againsed the shared object need to #include the shared object header file.

whats the point of having a shared object if the function body is also in the header !?

Compilers i know of that support this are MS Visual C++ ang GNU GCC.

anyways, i managed to find an example template class program which happened to have a few function boddy's outside of the class prototype.

the correct syntax for a function body outside of the prototype (for anyone else interested in the answer) on a templarte class is

Code:
template<class dataType>
returnType classname<dataType>::functionName(parameters) { 
      Function Body; 
}

Last edited by qwijibow; 12-12-2004 at 04:28 PM.
 
Old 12-13-2004, 09:28 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
While you don't have to have all the functions inline with the class declaration, when using template classes you DO need to have the full implementation of that class defined before it is used (e.g. in the header file). Object code for a template class is not produced until a specialization of that class is created. Therefore, the compiler needs to have a full implementation of a template class at the time of compilation.

Here's a thread where this has been discussed before.

Note that there is a way that the C++ spec allows you to separate the header and implementation via exported template, but to the best of my knowledge, there are only 1 or 2 specialized compilers that support this. Neither of which is g++ or VC++. (And at a MS TechEd session I once was at a couple years ago, I seem to recall the speaker saying that VC++ would probably never support this feature due to the complexity.)

Last edited by deiussum; 12-13-2004 at 09:43 AM.
 
Old 12-13-2004, 10:34 AM   #5
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
so if i understand correctly... ist impossible to put a template class in a .so shared object ?

at compile time of the application that uses the shared object always has the class' header file, with all the function prototypes, isnt this enough ???

ive almost finished codeing the cliant program for the shared object... but the template class template object compiled fine, g++ ididnt complain at all.

edit:
(jest read the link, and the link in the link)

makes sence now....

well this is annoying....

i use the same HUGE class in almost all of my Artificial inteligence courseworks !
i thought it would be great to dynamically link it, rather than compile i over and over and over...

ohh well, looks like i ve got to compile and statically link it to all my works.. lame

Last edited by qwijibow; 12-13-2004 at 10:40 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"undefined reference" to a template class function btb Programming 3 08-25-2005 06:02 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 09:27 PM
template class ckcheung0927 Programming 3 11-28-2004 04:59 PM
Question about function prototypes xailer Programming 4 12-15-2003 11:10 AM
C++ template class syntax rong Programming 12 08-13-2003 01:26 PM


All times are GMT -5. The time now is 03:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration