模板与函数指针应用有关问题(测试程序报错),高手,
模板与函数指针应用问题(测试程序报错),高手,求救!
#include "StdAfx.h "
#include "iostream.h "
class A{
private:
int value;
public:
A(){}
void setvalue(int &v){
this-> value=v;
}
void display(){
cout < < "value= " < <value < <endl;
}
};
template < class T , class U >
struct s{
typedef T tTyp;
typedef void (T::*memberfxntyp)(U &);
typedef void (T::*dis)();
memberfxntyp lpMemfn;
dis lpdis;
};
void main(){
A classA;
int v=5;
classA.setvalue(v);
s <A,int> s1;
s1.lpdis=A::display;
s1.lpdis();
}
========================================
s1.lpdis();
这句就会报错:
error C2064: term does not evaluate to a function
函数指针在这里应该怎么用啊``谢谢大哥,大姐们!!!
------解决方案--------------------
s <A,int> s1;
s1.lpdis=A::display;
s1.lpdis();
改成这样
s <A,int> s1;
s1.lpdis=&A::display;
(classA.*s1.lpdis)();
你还是看看C++Primer吧
------解决方案--------------------
(classA.*s1.lpdis)();
#include "StdAfx.h "
#include "iostream.h "
class A{
private:
int value;
public:
A(){}
void setvalue(int &v){
this-> value=v;
}
void display(){
cout < < "value= " < <value < <endl;
}
};
template < class T , class U >
struct s{
typedef T tTyp;
typedef void (T::*memberfxntyp)(U &);
typedef void (T::*dis)();
memberfxntyp lpMemfn;
dis lpdis;
};
void main(){
A classA;
int v=5;
classA.setvalue(v);
s <A,int> s1;
s1.lpdis=A::display;
s1.lpdis();
}
========================================
s1.lpdis();
这句就会报错:
error C2064: term does not evaluate to a function
函数指针在这里应该怎么用啊``谢谢大哥,大姐们!!!
------解决方案--------------------
s <A,int> s1;
s1.lpdis=A::display;
s1.lpdis();
改成这样
s <A,int> s1;
s1.lpdis=&A::display;
(classA.*s1.lpdis)();
你还是看看C++Primer吧
------解决方案--------------------
(classA.*s1.lpdis)();