I am fairly new to c++ and have not had much experience with headers or templates, and I have not had any experience with them combined. So, I have run into problems trying to use the class I have created. My class definitions and headers are as follows: J.h is the header file for the template class J.
#ifndef J_H
#define J_H
template<class t>
class J {
public:
void speak();
};
#endif
J.cpp has the function definitions for it's header.
#include "J.h"
#include <iostream>
template<class T> void J<T>::speak(){
std::cout << "Hello from j";
}
main.cpp has the main function and tries to use J.
#include "J.h"
int main(){
J<int> j;
j.speak();
}
My problem is that when I compile with g++ J.h J.cpp main.cpp
, I get the error undefined reference to J<int>::speak()
, but doing g++ J.h J.cpp
runs without any error. I am very new to templates, so any help is appreciated. Thank you in advance.