这个算了。。我在类模板里用。。。。下面是个杯具了。。。。

//main.cpp>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream>
#include "Mem.h"
using namespace std;
void aa(int &d)
{
    d=4;
}
void bb(int &d)
{
    static int i=1;
    d=i;
    i++;
}
void print(int *ptr,int N)
{
    for(int i=0;i<N;i++)
        cout<<"ptr["<<i<<"]= "<<ptr[i]<<endl;
}
int main()
{
    const int N=4;
    int *ptr=Mynew<int,N>(7);
    print(ptr,N);
    Mydelete(ptr);
    return 0;
}
//Mem.h>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#ifndef MEM_H
#define MEM_H
#include <cstdlib>
#include "Test.h"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#if 0
template<class T,int N>
T* Mynew(typename Test<T>::Fun init)
{
    T* tmp=static_cast<T*>(malloc(sizeof(T)*N));
    if(tmp)
    {
        Test<T> ft(init);
        for(int i=1;i<=N;i++)
            ft.Run(tmp[i-1]);
    }
    return tmp;
}
#endif
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#if 1
template<class T,int N>
T* Mynew(const T &v=T())
{
    T* tmp=static_cast<T*>(malloc(sizeof(T)*N));
    if(tmp)
        for(int i=1;i<=N;i++)
            tmp[i-1]=v;
    
    return tmp;
}
#endif
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#if 1
template<class T>
void Mydelete(T *&p)
{
    p->T::~T();
    free(p);
    p=0;
}
#endif
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#endif
//Test.h>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#ifndef TEST_H
#define TEST_H
template<class T>
class Test
{
public:
    typedef void (*Fun)(T&);
    explicit Test( Fun fun=0){Setfun(fun);}
    virtual ~Test(){}
    void Setfun(Fun fun){function=fun;}
    void Run(T &d){(*function)(d);}
private:
    
    Fun function;
};
#endif
>>******************************************************
1:在Mem中的第一个函数模板
不编译,第二个函数模板
编译的时候可以的
2:在Mem中的第一个函数模板
编译,第二个函数模板
不编译的时候就杯具了
3:在Mem中的第一个函数模板
编译,第二个函数模板
编译的时候就更杯具了。。。

4: Mem中的第一个函数模板要
调用到的函数在main.cpp中。。。就是 aa,bb

大家在自己的机子上试试,帮忙找找错误。。。