LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   function prototypes in template class' (c++) (https://www.linuxquestions.org/questions/programming-9/function-prototypes-in-template-class-c-265535/)

qwijibow 12-12-2004 08:52 AM

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.

Mara 12-12-2004 02:36 PM

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.

qwijibow 12-12-2004 03:26 PM

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;
}


deiussum 12-13-2004 08:28 AM

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.)

qwijibow 12-13-2004 09:34 AM

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 :(


All times are GMT -5. The time now is 04:01 PM.