如果我有一个非模板(即“普通”)类,并且希望拥有一个模板友元函数,我应该如何编写以避免导致编译器错误?以下示例可说明我的意图:
template <class T>
void bar(T* ptr);
class MyClass // note that this isn't a template class
{
private:
void foo();
template <class T>
friend void bar(T*); // ERROR: compiler gives me all kinds of grief
};
template <class T>
void bar(T* ptr)
{
if (ptr)
{
MyClass obj;
obj.foo();
}
}
我正在使用Visual Studio 2005,收到的具体错误是C2063错误,指出“bar”不是一个函数。这里需要做什么不同的事情呢?