Possible Duplicate:
C++ templates, undefined reference
I have a very simple program consisting of three files, which builds vectors from plain arrays:
//create.hpp
#ifndef CREATE_HPP_
#define CREATE_HPP_
#include <vector>
using namespace std;
template<class T>
vector<T> create_list(T uarray[], size_t size);
#endif /* CREATE_HPP_ */
//create.cpp
#include "create.hpp"
template<class T>
vector<T> create_list(T uarray[], size_t size){
vector<T> ulist(uarray, uarray + size);
return ulist;
}
//main.cpp
#include <vector>
#include <iostream>
#include "create.hpp"
using namespace std;
int main(){
char temp[] = { '/', '>' };
vector<char> uvec = create_list<char>(temp, 2);
vector<char>::iterator iter=uvec.begin();
for(;iter != uvec.end();iter++){
cout<<*iter<<endl;
}
return 0;
}
The build process is as follows:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o create.o create.cpp
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o main.cpp
g++ -o main.exe main.o create.o
While building the program, I get this error:
main.o: In function `main':
../main.cpp:18: undefined reference to `std::vector<char, std::allocator<char> > create_list<char>(char*, unsigned int)'
This program is really simple. However, the compilations passed successfully, but the link failed. Then I move all the code into a single file, and everything works like a charm. Can anybody help me figure this out?