I have a member function template of a class declared in source and header as
// c.h
class C {
public:
C(){}
template<typename T>
C& operator>>(T& obj);
};
and
// c.cpp
#include <iostream>
#include "c.h"
template<typename T>
C& C::operator>>(T& obj) {
std::cin >> obj;
return *this;
}
The main is in a separate file as:
#include <iostream>
#include "c.h"
int main() {
C c;
int n;
c >> n;
std::cout << n << std::endl;
}
Compiling these results in a link-time error
/usr/bin/ld: /tmp/ccY8edlI.o: in function `main':
main.cpp:(.text+0x23): undefined reference to `C& C::operator>><int>(int&)'
collect2: error: ld returned 1 exit status`
But moving the definition of >> from c.cpp to the file that has main compiles successfully. What's the rule here that causes it to fail in the first case? I looked around and the only related thing I found is a statement on MS docs Local classes are not allowed to have member templates.
I'm not sure what local classes are, but it sounds like the opposite of what I have.