Was originally working on a question someone else asked earlier: Why string is not printed?C++. Seeing that OP was not quite utilizing the template for DataOut
and GetData
, so I was trying to make them template as well.
Here's the code I end up having:
#include <iostream>
#include <string>
template<class T>
class Array{
public:
T U[10];
friend void DataOut(const Array&);
friend void GetData(Array&);
};
template<class T>
void DataOut(const Array<T>& arr){
std::cout << arr.U[0];
}
template<class T>
void Getdata(Array<T>& arr){
std::cin >> arr.U[0];
std::cin.clear();
}
int main(){
Array<std::string> Arr1;
Getdata(Arr1);
DataOut(Arr1);
}
However, I got undefined reference for DataOut
: main.cpp:(.text+0x3a): undefined reference to 'DataOut(Array<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
I found two ways to get around this:
- Define the
DataOut
inside theArray
class. - Call
DataOut<std::String>
in main, instead of justDataOut
.
The question is, how come no errors happens to GetData
? I also tried to call and define them in different orders, but still the same result.
Is there something I'm missing? Or it's my compiler's (clang 7.0.0
) fault?