problems with templates - undefined reference

When I compile and link the code for my interpreter's parser, the gcc compiler that came with codeblocks throws an error. I've narrowed it down to a function that uses templates. I've never used templates before (yes I know I have with vectors, but defining them is new) and I can't seem to fix it.

the code is from a few files:

load_program.cpp snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
            if(!line.empty() and (line[0] != '_' or line[0] != '$')) // if this holds then token_value must be a command or an operator
            {
                if(contains<std::string>(token_value, commands)) // must be a command
                {
                    program.push_back(token(token::command, token_value));
                    goto next_token;
                }
                else if(contains<std::string>(token_value, operators)) // must be an operator
                {
                    program.push_back(token(token::op, token_value));
                    goto next_token;
                }
                else
                {
                    error_and_terminate("unrecognised token");
                }


token_value and line are std::string
commands and operators are std::vector<std::string>
program is an std::vector<token> // I made the class token, but it is unrelated to the problem.

utility_functions.hpp snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef UTILITY_FUNCTIONS_HPP_INCLUDED
#define UTILITY_FUNCTIONS_HPP_INCLUDED

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>

extern void error_and_terminate(const char *s);

extern void usage();

template <class Tcontains>
extern bool contains(Tcontains elem, std::vector<Tcontains> vect); // similar to java's vector's member function


#endif // UTILITY_FUNCTIONS_HPP_INCLUDED 


utility_functions.cpp snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <cstdlib>
#include <string>
#include "utility_functions.hpp"

template <class Tcontains>
bool contains(Tcontains elem, std::vector<Tcontains> vect)
{
    for(unsigned i = 0; i < vect.size(); i++)
    {
        if(vect[i] == elem)
        {
            return true;
        }
    }
    return false;
}


Thanks in advance.
Last edited on
template function definitions hove to be visible to wherever you use them. That means you have to define the function in utility_functions.hpp.
Your contains function is the exact same thing as

iterator std::vector<T>::find( const T& val )

called as

vect.find( elem ) != vect.end();

Topic archived. No new replies allowed.