Template
-
Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou
-
Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou
When you split the two classes into two header files, did you remember to #include the dependent header file in the one that depends on it? If these are template classes, there shouldn't be any code in CPP files. Actual intances of the template class aren't created until an object of the class is defined, so at that point the compiler would need to see the code in the CPP file. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hello developers, Can i define a class in header file and declare it in .cpp file if using template or do i have to define and declare in the same header file. I have got two classes so do i have to define and declare both in the same header file or is it possible to have different header file for both classes. I am writing double linked list application. Now i have got two class LList and Node. Now when i simply use template and write the two classes under one file without defining it , then it works perfectly fine but when i try to define the two class seperately i am getting many errors. What should i do in this case ? Any way out ? ThankYou
james_dixon_2008 wrote:
Can i define a class in header file and declare it in .cpp file if using template
Read this -> http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12[^]
Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http:\\nibuthomas.wordpress.com
-
When you split the two classes into two header files, did you remember to #include the dependent header file in the one that depends on it? If these are template classes, there shouldn't be any code in CPP files. Actual intances of the template class aren't created until an object of the class is defined, so at that point the compiler would need to see the code in the CPP file. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thanks, yes i included the dependent header file but i got many errors. But when include the node class declaration and defintion and llist class declartion in one header file then the program works. But yes if i try to declare the llist class first and then define it, it gives me many error. So i came to the conclusion that i have included all the code under one header file. Declaring template node class and then defining all the functions and then directly defining the template llist class as it doesn't work when i first declare it and then define it. I guess that each template class could be placed in different header file to use by main method. But in my case it's not working. So i have only main class and the one header file containing two template class.
-
Thanks, yes i included the dependent header file but i got many errors. But when include the node class declaration and defintion and llist class declartion in one header file then the program works. But yes if i try to declare the llist class first and then define it, it gives me many error. So i came to the conclusion that i have included all the code under one header file. Declaring template node class and then defining all the functions and then directly defining the template llist class as it doesn't work when i first declare it and then define it. I guess that each template class could be placed in different header file to use by main method. But in my case it's not working. So i have only main class and the one header file containing two template class.
If the list class references the node class then the node class will always have to be defined before (in compile order) the list class, whether you use separate header files or not. The following is no different than using a single header file for both classes...
/////////////////
// node.h#pragma once
...define node class...
/////////////////
// list.h#pragma once
#include "node.h"
...define list class...
////////////////
// any file that uses the list class#pragma once
#include "list.h"
...Mark Salsbery Microsoft MVP - Visual C++ :java: