Template Functions

I'm sure that there's some simple fix for this, like a missing keyword or something, but I seem to be googling the wrong terms. Also, I should be putting this in the header, right? Since it's a template function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class HUD
{
    public:
        HUD();
        template <class T>
        void AppendToString(sf::String target, T parameter);
        sf::String ScoreText;
        sf::String HealthText;
};

// Template Function
void HUD::AppendToString(sf::String target, T parameter)
{
    std::stringstream mystr;
    mystream << parameter;
    target.SetText("Score: " + mystr.str());
}
It's hard to help without knowing what the problem is.
Ah, sorry.


C:\Users\...\Spacefighter\HUD.h|23|error: 'HUD::T' has not been declared|
C:\Users\...\Spacefighter\HUD.h|23|error: prototype for 'void HUD::AppedToString(sf::String, int)' does not match any in class 'HUD'
C:\Users\...\Spacefighter\HUD.h|11|error: candidate is: template<class T> void HUD::AppendToString(sf::String, T)


I'm using Code::Blocks
Add template <class T> just above line 12. All template functions have to be defined this way. Also remember that template functions have to be declared and defined in the same file.
Thanks, will do.
Topic archived. No new replies allowed.