Help with templates

Dec 31, 2012 at 11:34pm
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:

1
2
3
4
5
6
7
8
9
10
template<class T>
string CXMLString<T>::m_error[_XMLMAX] =
{ "XMLFULLLINE", "XMLOPENTAG", "XMLCLOSETAG", "XMLERRORSYNTAX",
		"XMLERRORTAGMISMATCH" };

template<class T>
string CXMLString<T>::error(XMLParseResult_t err)
{
	return m_error[err];
}


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?
Jan 1, 2013 at 8:55am
closed account (o3hC5Di1)
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
Jan 1, 2013 at 10:52am
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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <stdlib.h>
#include <sstream>
#include "CXMLStringTemplate.h"

using namespace std;
using namespace CXML;

template<class T>
string CXMLString<T>::m_error[_XMLMAX] =
{ "XMLFULLLINE", "XMLOPENTAG", "XMLCLOSETAG", "XMLERRORSYNTAX",
		"XMLERRORTAGMISMATCH" };

template<class T>
string CXMLString<T>::error(XMLParseResult_t err)
{
	return m_error[err];
}

template string error<string>(XMLParseResult_t err);


throws an error: symbol error could not be resolved
Jan 1, 2013 at 11:24am

TemplateClass.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef TEMPLATECLASS_H_
#define TEMPLATECLASS_H_

#include <string>

template <typename T>
class C
{
public:
    enum ErrorType { One, Two, Three } ;

    static std::string error(ErrorType err) ;

private:
    static std::string m_error[3] ;

};


template <typename T>
std::string C<T>::error( typename C<T>::ErrorType err )
{
    return m_error[err] ;
}

template <typename T>
std::string C<T>::m_error[3] = {"1", "2", "3" };

#endif 



Main.cpp:
1
2
3
4
5
6
7
8
#include <string>
#include <iostream>
#include "templateClass.h"

int main()
{
    std::cout << C<int>::error(C<int>::One) << '\n' ;
}
Jan 1, 2013 at 11:31am
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?
Topic archived. No new replies allowed.