C++ 为什么操作符重载不能正常工作

C++ 为什么操作符重载不能正常工作,c++,operator-overloading,C++,Operator Overloading,为了实现教育目标,为sys/socket编写类包装器 -这是一种方法。 -这是用法 因此,XCode抛出一个错误: Undefined symbols for architecture x86_64: "Socket& operator<<<char const [16]>(Socket&, char const (&) [16])", referenced from: _main in main.o ld: symbol(s) n

为了实现教育目标,为sys/socket编写类包装器

-这是一种方法。 -这是用法

因此,XCode抛出一个错误:

Undefined symbols for architecture x86_64:
  "Socket& operator<<<char const [16]>(Socket&, char const (&) [16])", 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)
尽管此代码编译和工作良好:

#include <iostream>
#include <string>

class test
{
public:
    void send(std::string str);
    template <class T>
    friend test &operator <<(test &tst, T &message);
};

void test::send(std::string str)
{
    std::cout << str;
}

template <class T>
test &operator <<(test &tst, T &message)
{
    tst.send(std::string(message));
    return tst;
}

int main()
{
    test t;

    t << "hello world!\n";
}

您需要将所有模板化的类函数定义移动到头文件中,或者将模板移动到.cpp文件中

请参阅,或者为什么要在此处使用模板?