This is my first time ever attempting to use class templates (I'm very new to C++)
I'm trying to create a very simple Number
class. To start, I'm creating a ToString
method. As of now, for testing purposes, I just want ToString
to return the string "testing"
.
When I run my code, I get the following error:
Undefined symbols for architecture x86_64:
"Number<int>::ToString()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [build/ml] Error 1
Here is my code, any help is appreciated:
main.cpp
#include "number.h"
int main(int argc, char* argv[]) {
Number<int> x(15);
x.ToString();
return 0;
}
number.h
#ifndef _NUMBER_
#define _NUMBER_
#include <iostream>
template <class T>
class Number {
private:
T m_val;
public:
Number(T val) : m_val(val) {};
std::string ToString();
};
#endif
number.cpp
#include "number.h"
template<class T>
std::string Number<T>::ToString() {
return std::string("testing");
}