Hi all:
I have a very odd problem:
In one header file I have
namespace ML_Mesh_NS {
/// Return the mean value of the vertex list
template <class T>
ML_Point GetCentre(const vector<T>& vertex_list);
}
And in a cpp file, there is the implementation of the function GetCentre.
using namespace ML_Mesh_NS;
template <class T>
ML_Point ML_Mesh_NS::GetCentre(const vector<T>& vertex_list){
...
}
When I call the function GetCentre from the main function, it works.
And there is another header file:
namespace ML_Mesh_NS{
template <class VT, class FT>
void read_mesh(const char* filename, vector<VT>& vl, vector<FT>& fl);
}
Again, the implementation is in another cpp file. But when I try to test the function, the compiler complains that "undefined reference to the function read_mesh".
using namespace ML_Mesh_NS;
template<class VT, class FT>
void ML_Mesh_NS::read_mesh(const char* filename, vector<VT>& vertex_list, vector<FT>& face_list){
...
}
Why the first one works but the second one doesn't work? I heard that the template code should always be in the header file, but why GetCentre works even the implementation is separated?
If I put the code in the header file, will the code be compiled in compile time? As I am worried the speed may be slow if it doesn't compile the code in the header file.
Thanks