注册 登录
编程论坛 C++教室

这个typedef 哪里错了。。。

尝鲜 发布于 2010-12-13 21:45, 729 次点击
typedef template<class T> void (*Ini)(T&,const T&=T());
就是弄一个函数模板的指针 不知道怎么就错了帮忙改一下啊。。。。
7 回复
#2
laoyang1032010-12-14 15:42
程序代码:
#include<iostream.h>
template<class T>
typedef void (*Ini)(T&,const T&);
void main()
{
    cout<<"123"<<endl;
}
这样就对了  格式书写问题  把template放在上面
#3
尝鲜2010-12-14 16:02
回复 2楼 laoyang103
不行的。。。看看、、
#include<iostream>
using namespace std;
template<class T>
typedef void (*Ini)(T&,const T&);
void aa(int &d,const int &v)
{
    d=v;
}
void main()
{
    cout<<"123"<<endl;
    int i=0;
    cout<<"i= "<<i<<endl;
    Ini ini;
    ini=aa;
    ini(i,3);
    cout<<"i= "<<i<<endl;
}
#4
pangding2010-12-14 18:18
typedef 不能用于模版。
#5
尝鲜2010-12-14 18:49
这个算了。。我在类模板里用。。。。下面是个杯具了。。。。
//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
大家在自己的机子上试试,帮忙找找错误。。。
#6
迷失的木桶2010-12-14 19:09
用仿函数方式构建,基本上可以解决你的问题。。。
#7
laoyang1032010-12-14 21:51
不要这样用  pangding大侠说的对   我调了很长时间   编译不过去的
程序代码:
#include<iostream>
using namespace std;
template<class T>
typedef void (*Ini)(T *d,T *v);
template<class T>
void aa(T *d,T *v)
{
    ;
}
void main()
{
    cout<<"123"<<endl;
    int i=0,j=3;
    cout<<"i= "<<i<<endl;
    Ini ini;
    ini=aa;
    aa(&i,&j);
    cout<<"i= "<<i<<endl;
}

你的aa函数里面只要一写东西  肯定报错
#8
尝鲜2010-12-14 22:39
杯具啊。。。。。。。。
下面的附件是个杯具的简化。。。
只有本站会员才能查看附件,请 登录

。。等待啊
1