求助:自定义foreach 函数报错

Injection 发布于 2012/03/23 21:59
阅读 231
收藏 0
template<class T>
struct CListNode
{
	CListNode<T>* prev;
	CListNode<T>* next;
	T value;
};

template<class T>
class CTList
{
public:
	CTList()
	{
		m_plistHead = m_plistTail = NULL;
		m_nCount = 0;
	}
	~CTList()
	{
		
	}

        template<class R,class F,class P>
	int foreach(F f,const P& p)
	{
		typedef R (T::*mpfn)(P p);
		mpfn pfn = (mpfn)f;

		CListNode<T>* pNode = m_plistTail;
		while (pNode)
		{
			pNode->value->pfn(p);
			pNode = pNode->prev;
		}
		return 1;
	}
}                  
以上是CTList定义的一部分  ,以下是测试部分.
class CTest2
{
public:
	CTest2(){printf("CTest2::CTest2\n");}
	~CTest2(){printf("CTest2::~CTest2\n");}
	long Shows(char* pStream){return printf("CTest2::Shows(%s)\n",pStream);}
	int Showd(int d){return printf("CTest2::Showd(%d)\n",d);}
};
void Test2()
{
	CTList<CTest2*> list;

	CTest2* pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	char* pStream = "123456";
	list.foreach<int,CTest2::Showd,int>(&CTest2::Showd,1);

	int nSize = list.size();
	for(int i=0;i<nSize;i++)
	{
		pTest = list.pop();
		while (pTest)
		{
			delete pTest;
			pTest = list.pop();
		}
	}
}
测试时报错,错误信息  Test.cpp(75) : error C2062: type 'int' unexpected
VC6 编译错误提示 
上图是错误的地方,不知道为什么会错误,搞不清除错误提示是什么意思,求达人解释一样.

加载中
0
头头木

是不是因为,int是基本类型,不是引用类型?

0
Injection
Injection
template<class T>
struct CListNode
{
	CListNode<T>* prev;
	CListNode<T>* next;
	T value;
};

template<class T>
class CTList
{
public:
	CTList()
	{
		m_plistHead = m_plistTail = NULL;
		m_nCount = 0;
	}      
        template<class R,class F>
	int foreach(R (F::*mpfn)(const T& t),F* pf)
	{
		CListNode<T>* pNode = m_plistTail;
		while (pNode)
		{
			pf->mpfn(pNode->value);
			pNode = pNode->prev;
		}
		return 1;
	}
private:
        CListNode<T>*	m_plistHead;
	CListNode<T>*	m_plistTail;
	int				m_nCount;
};
void Test2()
{
	CTest3	test3;
	CTList<CTest2*> list;

	CTest2* pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	pTest = new CTest2();
	list.push(pTest);

	list.foreach<CTest3>(&CTest3::Show,&test3);

	int nSize = list.size();
	for(int i=0;i<nSize;i++)
	{
		pTest = list.pop();
		while (pTest)
		{
			delete pTest;
			pTest = list.pop();
		}
	}
} 
报错的地方  list.foreach<CTest3>(&CTest3::Show,&test3); 提示:error C2275: 'CTest3' : illegal use of this type as an expression,求解


返回顶部
顶部