Basic doubt with Class template
-
Hello everyone Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file. I know the way, wherein, we define all the methods in a file( with extension other then .cpp), and include the file at the end of the header file of the template class. But this method looks no good, is there any clean and usual way just like other class. thanks in advance Hrishi
-
Hello everyone Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file. I know the way, wherein, we define all the methods in a file( with extension other then .cpp), and include the file at the end of the header file of the template class. But this method looks no good, is there any clean and usual way just like other class. thanks in advance Hrishi
The reason for placing definitions in a header file is so that they can be included in many implementation (
.cpp
) files. If your definitions are only used within one implementation file then you can put them at the top of that file. Or did I misunderstand your question?The best things in life are not things.
-
Hello everyone Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file. I know the way, wherein, we define all the methods in a file( with extension other then .cpp), and include the file at the end of the header file of the template class. But this method looks no good, is there any clean and usual way just like other class. thanks in advance Hrishi
Question is not very clear as to what you're trying to do, try clarifying what you're trying to accomplish.
-
The reason for placing definitions in a header file is so that they can be included in many implementation (
.cpp
) files. If your definitions are only used within one implementation file then you can put them at the top of that file. Or did I misunderstand your question?The best things in life are not things.
Yes sure, thanks . .But I want to know is there any way that I can separate the definitions of methods, in a .cpp file, (just like what we do for normal classes) . . . .[I tried , although there is no syntax error, but it shows linking errors]........ FOR SOME REASON , I DON'T WANT TO PUT THE METHOD'S DEFINITIONS IN .h FILE......... thanks again hrishi
-
Question is not very clear as to what you're trying to do, try clarifying what you're trying to accomplish.
thanks for ur reply.... ok, I have a class template with few methods....according to my knowledge, we have to put the definitions of all template-class-methods in .h files itself .....actually , FOR SOME REASON , I DON'T WANT TO PUT THE METHODS DEFINITIONS IN .h FILE.......i want to put them in .cpp file, just like other normal classes that we do......is it possible? if so, how?.... thanks in advance Hrishi
-
Yes sure, thanks . .But I want to know is there any way that I can separate the definitions of methods, in a .cpp file, (just like what we do for normal classes) . . . .[I tried , although there is no syntax error, but it shows linking errors]........ FOR SOME REASON , I DON'T WANT TO PUT THE METHOD'S DEFINITIONS IN .h FILE......... thanks again hrishi
hrishi321 wrote:
but it shows linking errors
That has nothing to do with whether your source is in a
.h
or.cpp
file. Link errors occur when you have references to objects or methods which do not exist within your program.hrishi321 wrote:
FOR SOME REASON , I DON'T WANT TO PUT THE METHOD'S DEFINITIONS IN .h FILE
1. Please do not SHOUT, we can read in normal case perfectly well. 2. You can put your definitions anywhere you like. The only requirement is that the compiler can find them when it comes across a reference in the source code. For example a statement of the form:
CThing thing = new CThing();
requires a definition of
CThing
, and that definition must already exist in the compiler's dictionary, having been found either in an included.h
file, or previously in the current source.cpp
file.The best things in life are not things.
-
hrishi321 wrote:
but it shows linking errors
That has nothing to do with whether your source is in a
.h
or.cpp
file. Link errors occur when you have references to objects or methods which do not exist within your program.hrishi321 wrote:
FOR SOME REASON , I DON'T WANT TO PUT THE METHOD'S DEFINITIONS IN .h FILE
1. Please do not SHOUT, we can read in normal case perfectly well. 2. You can put your definitions anywhere you like. The only requirement is that the compiler can find them when it comes across a reference in the source code. For example a statement of the form:
CThing thing = new CThing();
requires a definition of
CThing
, and that definition must already exist in the compiler's dictionary, having been found either in an included.h
file, or previously in the current source.cpp
file.The best things in life are not things.
-
thanks a lot, for all the points. But i am sorry, I really didn't get my answer... so does it mean to say, that I can put my class(template) method definitions in .cpp file?? Regards, hrishi
-
thanks for ur reply.... ok, I have a class template with few methods....according to my knowledge, we have to put the definitions of all template-class-methods in .h files itself .....actually , FOR SOME REASON , I DON'T WANT TO PUT THE METHODS DEFINITIONS IN .h FILE.......i want to put them in .cpp file, just like other normal classes that we do......is it possible? if so, how?.... thanks in advance Hrishi
You don't have to put the definitions in the header, just the declarations. In header file:
template class CMyTemplate
{
public:
virtual void Method1(void*);
};In cpp file:
template void CMyTemplate::Method1(void*){}
-
You don't have to put the definitions in the header, just the declarations. In header file:
template class CMyTemplate
{
public:
virtual void Method1(void*);
};In cpp file:
template void CMyTemplate::Method1(void*){}
Hello again :) thanks for ur reply... yes, so say in this program, here is the problem i am facning... Now in the main function, if I try to create an object . .like CMyTemplate<int> objT; it gives me linking error error LNK2001: unresolved external symbol "public: virtual void __thiscall CMyTemplate<int>::Method1(void *)" (?Method1@?$CMyTemplate@H@@UAEXPAX@Z) waiting for your reply.. thanks hrishi
-
hrishi321 wrote:
does it mean to say, that I can put my class(template) method definitions in .cpp file?
Yes; I thought I had explained this in my previous two answers; obviously my explanations need some work.
The best things in life are not things.
-
Hello again :) thanks for ur reply... yes, so say in this program, here is the problem i am facning... Now in the main function, if I try to create an object . .like CMyTemplate<int> objT; it gives me linking error error LNK2001: unresolved external symbol "public: virtual void __thiscall CMyTemplate<int>::Method1(void *)" (?Method1@?$CMyTemplate@H@@UAEXPAX@Z) waiting for your reply.. thanks hrishi
-
Where is the code for
Method1()
? I find it rather difficult to understand why you are using templates when you seem not to have mastered the basics of classes yet.The best things in life are not things.
Code In xxx.h file ::>> -------------------------------------- template <class T> class CMyTemplate { public: virtual void Method1(T TDummy); }; Code in xxx.cpp file ::>> -------------------------------------- #include "xxx.h" template <class T> void CMyTemplate<T>::Method1(T TDummy) { TDummy = TDummy + 1; } Code in main function ::>> -------------------------------------- CMyTemplate<int> objT;
-
Code In xxx.h file ::>> -------------------------------------- template <class T> class CMyTemplate { public: virtual void Method1(T TDummy); }; Code in xxx.cpp file ::>> -------------------------------------- #include "xxx.h" template <class T> void CMyTemplate<T>::Method1(T TDummy) { TDummy = TDummy + 1; } Code in main function ::>> -------------------------------------- CMyTemplate<int> objT;
I cannot see anything wrong with this, and I have copied it into a test program of my own and it compiles and links successfully. There must be something different in your implementation that you have not shown in your message. Also, please surround your code snippets with <pre> tags (use the code block button) so it shows more clearly, like this:
template void CMyTemplate::Method1(T TDummy)
{
TDummy = TDummy + 1;
}The best things in life are not things.
-
I cannot see anything wrong with this, and I have copied it into a test program of my own and it compiles and links successfully. There must be something different in your implementation that you have not shown in your message. Also, please surround your code snippets with <pre> tags (use the code block button) so it shows more clearly, like this:
template void CMyTemplate::Method1(T TDummy)
{
TDummy = TDummy + 1;
}The best things in life are not things.
thanks for your reply and your suggestions, Oh, I am really unable to figure it out. And there is no difference as such in my implementation. FYI, the linking error which i am getting is..
TestProject.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CMyTemplate<int>::Method1(int)" (?Method1@?$CMyTemplate@H@@UAEXH@Z)
modified on Thursday, July 7, 2011 10:35 PM
-
thanks for your reply and your suggestions, Oh, I am really unable to figure it out. And there is no difference as such in my implementation. FYI, the linking error which i am getting is..
TestProject.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CMyTemplate<int>::Method1(int)" (?Method1@?$CMyTemplate@H@@UAEXH@Z)
modified on Thursday, July 7, 2011 10:35 PM
I have no idea what you are doing wrong, as you have not shown your code. Here is an implementation that works; just copy this code and paste into a single
.cpp
file and build it.#include <iostream>
using namespace std;
template class CMyTemplate
{
public:
virtual void Method1(T& TDummy);
};template void CMyTemplate::Method1(T& TDummy)
{
TDummy = TDummy + 1;
}int wmain()
{
int zz = 25;
CMyTemplate objT;
objT.Method1(zz);
wcout << L"Value of the dummy: " << zz << endl;return 0;
}
The best things in life are not things.
-
I have no idea what you are doing wrong, as you have not shown your code. Here is an implementation that works; just copy this code and paste into a single
.cpp
file and build it.#include <iostream>
using namespace std;
template class CMyTemplate
{
public:
virtual void Method1(T& TDummy);
};template void CMyTemplate::Method1(T& TDummy)
{
TDummy = TDummy + 1;
}int wmain()
{
int zz = 25;
CMyTemplate objT;
objT.Method1(zz);
wcout << L"Value of the dummy: " << zz << endl;return 0;
}
The best things in life are not things.
I think there is a misunderstanding. Sorry if my words were not able to explain my doubt. If you please refer to my main-doubt
Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file.
The code which you have provided works absolutely fine, in just one file. Just that I wanted to know, if there is any way to separate the definitions of the method in an another .cpp file. (Just like a normal class where we declare in .h and implement in .cpp)
template <class T>
void CMyTemplate<T>::Method1(T& TDummy)
{
TDummy = TDummy + 1;
}and the declaration in an separet .h file
template <class T>
class CMyTemplate
{
public:
virtual void Method1(T& TDummy);
};thanks in advance hrishi
-
I think there is a misunderstanding. Sorry if my words were not able to explain my doubt. If you please refer to my main-doubt
Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file.
The code which you have provided works absolutely fine, in just one file. Just that I wanted to know, if there is any way to separate the definitions of the method in an another .cpp file. (Just like a normal class where we declare in .h and implement in .cpp)
template <class T>
void CMyTemplate<T>::Method1(T& TDummy)
{
TDummy = TDummy + 1;
}and the declaration in an separet .h file
template <class T>
class CMyTemplate
{
public:
virtual void Method1(T& TDummy);
};thanks in advance hrishi
OK, I think I understand what you are asking. You can put the definition and the implementation into a single
.cpp
file, but it must all be in the same file - as was the case with my sample. You cannot put the definition into one.cpp
file and the implementation into another, as the information required by the compiler must be available in one compilation unit. It is much simpler to follow the default and put your definitions into a.h
file and your implementation into the.cpp
file(s). Doing it this way means that you can split the implementation across multiple files, as long as each one includes the definitions.The best things in life are not things.
-
Hello everyone Could anyone please tell me, is there any way to separate the definition of the methods of a template class ,from header file to .cpp file. I know the way, wherein, we define all the methods in a file( with extension other then .cpp), and include the file at the end of the header file of the template class. But this method looks no good, is there any clean and usual way just like other class. thanks in advance Hrishi
Templates behave a lot like inline functions. The entire implementation is required to be declared in the include file for the compiler to use it correctly. I believe you have described this method in your question, but I would like to start here, because it is the method I prefer if I dont want a single .h file implementation. I will use the file name Tmp.x for all of the examples, where x is the extension. 1) You declare all of your template classes and functions in your file Tmp.h . 2) Then implement all of the functions in a separate file with an extension such as Tmp.inl (inline). It is important that when the function definitions appear outside of the class definition, that you declare each function declaration with inline. Otherwise if you include this header file in multiple places, you will get linker errors for duplicate symbols. 3) At the bottom of Tmp.h and the line #include "Tmp.inl". The file implemented in step 2. If you prefer to have the file extension to still be .cpp, you can use that as well. You would follow all of the steps the same way, and include the .cpp file at the end of the .h file. The only difference, in your Visual Studio project, you will have to change the settings for the file so that it does not get compiled like the other .cpp files. It is already included in a header file. You cannot get around having the Template implementation available to the compiler when it sees the first usage of the template. Why? Because that is the point where the compiler generates the actual code from the template. Originally there was supposed to be a keyword "export" to allow the templates to be defined like regular classes, but that never became part of the standard, and therefore is not available. Hopefully this helps.
-
Templates behave a lot like inline functions. The entire implementation is required to be declared in the include file for the compiler to use it correctly. I believe you have described this method in your question, but I would like to start here, because it is the method I prefer if I dont want a single .h file implementation. I will use the file name Tmp.x for all of the examples, where x is the extension. 1) You declare all of your template classes and functions in your file Tmp.h . 2) Then implement all of the functions in a separate file with an extension such as Tmp.inl (inline). It is important that when the function definitions appear outside of the class definition, that you declare each function declaration with inline. Otherwise if you include this header file in multiple places, you will get linker errors for duplicate symbols. 3) At the bottom of Tmp.h and the line #include "Tmp.inl". The file implemented in step 2. If you prefer to have the file extension to still be .cpp, you can use that as well. You would follow all of the steps the same way, and include the .cpp file at the end of the .h file. The only difference, in your Visual Studio project, you will have to change the settings for the file so that it does not get compiled like the other .cpp files. It is already included in a header file. You cannot get around having the Template implementation available to the compiler when it sees the first usage of the template. Why? Because that is the point where the compiler generates the actual code from the template. Originally there was supposed to be a keyword "export" to allow the templates to be defined like regular classes, but that never became part of the standard, and therefore is not available. Hopefully this helps.