I'm currently learning how to overload the stream insertion operator for my class. Found below is the code I have:
#include <iostream>
#include <string>
using std::string;
using std::endl;
using std::cout;
template<typename T>
class A {
public:
A(T);
friend std::ostream& operator<<(std::ostream&, const A&);
T* value;
};
template<typename T>
A<T>::A(T input) {*value = input;}
template<typename T>
std::ostream& operator<<(std::ostream& out, const A<T>& obj) {
out << *(obj.value);
return out;
}
int main() {
A<string> foo("HELLO");
cout << foo << endl;
return 0;
}
I'm getting this error after compiling the code with G++
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, A<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&)", referenced from:
_main in streamops-ef073c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea what could be wrong? I'm following a guide and don't see the difference in my operator overload.