Template Functions

Oct 23, 2010 at 11:53pm
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());
}
Oct 24, 2010 at 12:32am
It's hard to help without knowing what the problem is.
Oct 24, 2010 at 12:37am
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
Oct 24, 2010 at 12:59am
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.
Oct 24, 2010 at 1:19am
Thanks, will do.
Topic archived. No new replies allowed.