I obtained a .hpp file online which contained both the class declaration and definition together as such:
//Foo.hpp
class Foo{
Foo();
<template T>
Foo& func1(const &T t);
};
inline Foo::Foo(){
//constructor
}
<template T>
Foo& Foo::func1(const T& t){
//function
}
I have to include this header file (basically a library) in multiple source files. Since it contains the definitions of the methods, I get redeclaration (or redefinition, I forgot) errors when I include it more than once in the project. So I decided to split the definitions into a .cpp file and I'm using the library in main.cpp as shown.
//Foo.hpp
class Foo{
Foo();
<template T>
Foo& func1(const T& t);
};
and
//Foo.cpp
#include "Foo.h"
Foo::Foo(){
//constructor
}
<template T>
Foo& Foo::func1(const T& t){
//function
}
For the function call
//main.cpp
#include "Foo.h"
int main (void){
Foo bar;
bar.func1(5); //shows error as "undefined reference to"
bar.func1<int>(5); //doesn't work either
}
From what I've found, I think that the acceptable types for the template should be declared by instantiating the template. However I don't know the syntax and the location to do so. Thanks in advance for any assistance.
FYI: just as a temporary workaround, I have explicitly stated the argument types as "int" for func1() in both the prototype in Foo.hpp and definition in Foo.cpp and getting rid of the template all together. No errors returned.
This is just temporary as there are many other similar templated functions as func1() and it does not seem like a pretty way to solve the issue. I strongly believe that "int" should be declared as one of the data types for template T, i just don't know how.