【发布时间】:2012-09-07 02:28:27
【问题描述】:
这是一个非常短的 sn-p,它不能用 g++ 4.7.1 编译(顺便说一下,它也不能用 gcc 4.6.3 编译)。
#include <iostream>
template<typename T>
struct Foo
{
template<typename U>
friend std::ostream& operator<<(Foo&, U&);
};
template<typename T, typename U>
std::ostream& operator<<(Foo<T> foo, U& u)
{
std::cout << u;
return std::cout;
}
int main()
{
Foo<int> f;
f << "bar";
return 0;
}
这就是 gcc 4.7.1 的输出(4.6.3 说的几乎相同)。
/tmp/ccNWJW6X.o: 在函数
main': main.cpp:(.text+0x15): undefined reference tostd::basic_ostream >& 运算符
谁能解释一下原因?
编辑
我也尝试过使用 clang 3.1,它说的完全一样。
【问题讨论】:
标签: c++ templates gcc linker template-function