templates
.-Hi, I have this problem:
.-I have a class CFile and need its method Write() use
templates to write whatever is needed... I have this code:
***in the class header:
template void Write(T x);
***in the .cpp:
template void DECFile::Write(T x)
{
m_fsFStream<<x;
}
.-The problem is that when linking it gives one error for each Write() call, this way:"public: void __thiscall DECFile::Write(int)" (?Write@DECFile@@QAEXH@Z)" if called Write with an integer parameter and so on....
.-I´ve done the same but without classes, and the template function works... What´s wrong?
What the hells!
What the hells!
My first question to you is why are you trying to use template methods?
It appears that you can get the same result using a virtual method Write().
I have not seen the rest of your CFile implementation, nor the implementation of DECFile, so i can not say anything about your design. But, it appears that using a template here doesn''t fit the requirements of what you are trying to accomplish.
Alek
It appears that you can get the same result using a virtual method Write().
I have not seen the rest of your CFile implementation, nor the implementation of DECFile, so i can not say anything about your design. But, it appears that using a template here doesn''t fit the requirements of what you are trying to accomplish.
Alek
.-First, thanks for replying...
.-Well CFile is DECFile sorry for the typpo...
.-Let me explain,I´m creating DECFile to use an interface as Pascal´s with files,Write(),Read(),Reset(),Eoln()....
.-I´ve planned to have DECFile as the geenric FileClass, that can be either text or binary... Then derive DECTextFile and any DEC????Binary from it as needed:If I´m going to have a file of structs of TSprite, for example,create DECSpriteFile derived from DECFile...
.-I want to have generical Write() and Read() that can be used with any data, so the only way are templates or lots of overloaded functions... I prefer templates.
Please help!
What the hells!
What the hells!
I think I know what he wants to do (and I think it''s a good idea). He could do
int i;
float f;
struct /* something complex */ s;
file.Write(i);
file.Write(f);
file.Write(s);
He would not have to change a bit of code for different data types, because the compiler could do all that (after all the only ''real'' difference is their size in memory).
I have not tried something like that for a long time but I remember a lot of compilers having some problems with member template functions. The code you posted has a few errors, but I think they are from pasting them incomplete here:
First, you missed the template in the declaration.
Second, I would use an inline implementation until I know the compiler can do what I want - I''m never sure about declaring templates outside the class - so maybe the error comes from there.
Third, use const T & data or T & data. This way a huge structure can be passed with a 4 byte pointer instead of copying the structure.
Fourth, if nothing of that helped I have run out of ideas.
Good luck.
int i;
float f;
struct /* something complex */ s;
file.Write(i);
file.Write(f);
file.Write(s);
He would not have to change a bit of code for different data types, because the compiler could do all that (after all the only ''real'' difference is their size in memory).
I have not tried something like that for a long time but I remember a lot of compilers having some problems with member template functions. The code you posted has a few errors, but I think they are from pasting them incomplete here:
class CFile{// ... template <class T> void Write(const T & data) { // do what you want }}
First, you missed the template in the declaration.
Second, I would use an inline implementation until I know the compiler can do what I want - I''m never sure about declaring templates outside the class - so maybe the error comes from there.
Third, use const T & data or T & data. This way a huge structure can be passed with a 4 byte pointer instead of copying the structure.
Fourth, if nothing of that helped I have run out of ideas.
Good luck.
.-Thanks for the replay....
.-First I paste this wrong:
***header***
template void Write(T x);
*****implementation*****
template void DECFile::Write(T x)
{
m_fsFStream<
}
.- I´ve tried doing the implementation in the heaader... It works!!!!!!! Thanks a lot!
.- About passing having reference parameters, it depends of what to read, in the text files I´m doing this way, an integer, a char.. are less than 4 bytes.Strings are 4 bytes this way also.
.-With binaries of a complex type al do reference parameter passing, so I´ll put write as virtual...
.-Thanks again!
What the hells!
What the hells!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement