undefined reference to template function
Oct 21, 2013 at 5:51pm UTC
Hi guys,
I am coding a RTS game but I cant compile my dataLoader function. It gives that errors when want to call it:
obj\Debug\FileSystem\getBackground.o||In function `Z13getBackgroundv':|
undefined reference to `bool dataLoader<int>(int&, std::string, std::string)'
undefined reference to `bool dataLoader<std::string>(std::string&, std::string, std::string)'
obj\Debug\main.o||In function `main':|
undefined reference to `bool dataLoader<int>(int&, std::string, std::string)'
undefined reference to `bool dataLoader<int>(int&, std::string, std::string)'
and here is my code:
a part of main.cpp:
1 2 3 4 5 6 7 8
//Window size
int width;
int height;
if ( !dataLoader<int >( width, "settings/resolution.txt" , "width" ) || !dataLoader<int >( height, "settings/resolution.txt" , "height" ) )
{
return -1;
}
getBackground.cpp:
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
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include "../func_declerations.h"
#include <string>
sf::Texture getBackground()
{
int number_of_backgrounds;
int chosen;
sf::Texture chosenTexture;
dataLoader<int >( number_of_backgrounds, "settings/backgrounds" , "number" );
std::string backgrounds[ number_of_backgrounds ];
for ( int i = 0; i < number_of_backgrounds; ++i )
{
dataLoader<std::string>( backgrounds[ i ], "settings/backgrounds" , "background" + i );
}
std::srand( time(NULL) );
chosen = ( (std::rand()) % number_of_backgrounds );
chosenTexture.loadFromFile( backgrounds[ chosen ] );
return chosenTexture;
}
Settings.cpp (File name should be changed)
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 30
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "../func_declerations.h"
template < typename T >
bool dataLoader( T &value, std::string directory, std::string search )
{
std::ifstream load( directory );
std::string part_of_content;
if ( !load.is_open() )
{
return false ;
}
while ( load >> part_of_content )
{
if ( part_of_content == search )
{
load >> value;
return true ;
}
}
return false ;
}
func_declerations.h
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef FUNC_DECLERATIONS_H_INCLUDED
#define FUNC_DECLERATIONS_H_INCLUDED
#include <SFML/Graphics.hpp>
#include <string>
template < typename T >
bool dataLoader( T &value, std::string directory, std::string search );
sf::Texture getBackground( );
#endif // FUNC_DECLERATIONS_H_INCLUDED
Oct 21, 2013 at 6:07pm UTC
Just call it like dataLoader(int , string1, string2)
Oct 22, 2013 at 2:14pm UTC
Thanks for help but that doesnt work.
Oct 22, 2013 at 2:36pm UTC
Oct 22, 2013 at 4:06pm UTC
The compiler needs to have access to the entire template definition at the point where it compiles the function call. You therefore need to include the entire template function definition in the header, not just the prototype.
Oct 22, 2013 at 5:36pm UTC
@ne555, uh how?
Oct 22, 2013 at 5:38pm UTC
I would have to say you problem is as mikey said. You must define the templated functions in the header not in the implementation file.
Oct 23, 2013 at 2:02pm UTC
Thanks for helps. It worked with Mikey's way.
Topic archived. No new replies allowed.