I have some code:
file.h:
#ifndef SAKE_H_INCLUDED
#define SAKE_H_INCLUDED
template <class T>
void swapq(T& a, T& b);
#endif // SAKE_H_INCLUDED
file.cpp:
#include "file.h"
template <class T>
void swapq(T& a, T& b){
T temp=a;
a=b;
b=temp;
}
main.cpp:
#include <iostream>
#include "file.h"
using namespace std;
int main()
{
int a=3, b=2;
swapq(a,b);
cout<<a<<" "<<b<<endl;
return 0;
}
So when I built it, the compiler show me a error:
||=== Build: Release in test (compiler: GNU GCC Compiler) ===|
obj/Release/main.o||In function `main':|
main.cpp:(.text.startup+0x2d)||undefined reference to `void
swapq<int>(int&, int&)'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0
second(s)) ===|
I deleted "template " and replaced "T" in void function with "int" and it built successfully. How can I use template in a "file.h" and "file.cpp" so that I can swap two varible with different datatypes ?