I am attempting to learn templated classes and functions, and figured with a templated class I could learn both easily.
main.cpp -
Code:
#include <iostream>
#include <string>
#include "newtemplate.h"
int main() {
newtemplate<int> test(5);
newtemplate<std::string> test2;
test2.print("\nblah");
return 0;
}
newtemplate.h -
Code:
#ifndef NEWTEMPLATE_H
#define NEWTEMPLATE_H
template <class type>
class newtemplate {
public:
newtemplate() {};
newtemplate(type a);
~newtemplate() {};
void print(type a);
};
#endif //NEWTEMPLATE_H
newtemplate.cpp -
Code:
#include <iostream>
#include "newtemplate.h"
template <class type> newtemplate<type>::newtemplate(type a) {
std::cout<<a;
}
template <class type> void newtemplate<type>::print(type a) {
std::cout<<a;
}
C:\DATA\development\cpp\newtemplate\newtemplate\ma in.cpp:6: undefined reference to `newtemplate<int>::newtemplate(int)'
C:\DATA\development\cpp\newtemplate\newtemplate\ma in.cpp:8: undefined reference to `newtemplate<std::string>:
rint(std::string)'
:: === Build finished: 2 errors, 0 warnings ===
I assume this is how it should be written from the FAQ on this site and a couple others... could someone point out my errors?