类模板和函数模板问题,具有挑战性!有经验和兴趣着进!!!
// header.h
#include <iostream>
using namespace std;
template <class T> void counts();
template <class TT>
class HasFriendT
{
private:
TT item;
static int ct;
public:
HasFriendT (const TT& i):item(i) {ct++;}
~HasFriendT () {ct--;}
friend void counts<TT> (); // 1.
};
template <class T>
int HasFriendT<T>::ct=0;
template <class T>
void counts()
{
cout <<"template counts():" << HasFriendT<T>::ct <<endl;
}
// use.cpp
#include "header.h"
int main()
{
counts<int>();
HasFriendT<int> hfi1 (10);
HasFriendT<int> hfi2 (20);
HasFriendT<double> hfd (10.5);
counts<double> ();
counts<int> ();
return 0;
}
按照上述编译,指示标记1.的代码行有错误!!若是改之为friend void counts (); 则编译正常!!
哪个老兄能解释一下??感觉C++标准里第一种写法是正确的牙!怎么会出错呢??
我用的是VC6的编译环境!