| noisycoder (49) | |||
|
Hi!! i'm using templates for the first time and trying to implement it in one the programs which had many constructors taking different types to convert it into a string. I've got most of the part correct and also I read on one of the forums that the constructor and other member functions should be declared in the header file and not in the .cpp file. Now the problem is I have a static function that returns a string. The code in .cpp file is:
but if i'm calling this method from main() i'm getting an error saying undefined address. How can I implement this in the header file itself since I cannot initialize the array within the class? | |||
|
|
|||
| NwN (770) | |
|
Hi there, Template functions / classes need to be defined in the header files. This explains why: http://www.parashift.com/c++-faq-lite/templates-defn-vs-decl.html It also links to a few possible alternative solutions. Hope that helps. All the best, NwN | |
|
|
|
| noisycoder (49) | |||
Well I understand that, but if I'm choosing the first option of keeping it all in the header file how do i initialize the member attributes of the class. Also, trying the second method and keeping the method definition in another .cpp file like:
throws an error: symbol error could not be resolved
| |||
|
|
|||
| cire (2347) | |||||
|
TemplateClass.h:
Main.cpp:
| |||||
|
|
|||||
| noisycoder (49) | |
| Hi! thanks for that code. However in the mean while what I tried was doing a #include of the .cpp file itself in my main() since I thought its just a linking error and not anything to do with compilation. Now it is working fine, but just want to know is it legit to do this. Is it a good programming technique or is the bit of code given by you a better solution? | |
|
|
|