不能将参数 1 从“CDecisionMakingx *const ”转换为“const CCounterTimerListener *”
用2008编译文件时出现!
1>j:\example\source files\decisionmakingx.cpp(29) : error C2664: “CCounterTimer::AttachListener”: 不能将参数 1 从“CDecisionMakingx *const ”转换为“const CCounterTimerListener *”
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>j:\example\source files\decisionmakingx.cpp(30) : error C2664: “CCounterTimer::AttachListener”: 不能将参数 1 从“CDecisionMakingx *const ”转换为“const CCounterTimerListener *”
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>j:\example\source files\decisionmakingx.cpp(32) : error C2664: “CCounterTimer::AttachListener”: 不能将参数 1 从“CDecisionMakingx *const ”转换为“const CCounterTimerListener *”
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
这里出的问题:
m_timerStart.AttachListener( this);
m_timerBallKick.AttachListener(this);
for (int i=0; i<ROBOTNUMBER; ++i)
m_timerShootLine[i].AttachListener(this);
那个计时器的源程序:
/////CCounterTimer.h//////
class CCounterTimer;
class CCounterTimerListener
{
public:
virtual void Update(CCounterTimer* Timer) = 0;
};
class CCounterTimer
{
public:
CCounterTimer();
virtual ~CCounterTimer();
/// Copying not allowed
CCounterTimer(const CCounterTimer &);
CCounterTimer &operator = (const CCounterTimer &);
void AttachListener(CCounterTimerListener* pListener);
void Step();
void Stop();
void Start(UINT nCount);
BOOL IsRuning() { return m_bRunning; };
private:
UINT m_nCount;
BOOL m_bRunning;
CCounterTimerListener* m_pListener;
};
///////CCounterTimer.cpp////////
CCounterTimer::CCounterTimer()
: m_nCount(0), m_bRunning(FALSE)
{
}
CCounterTimer::~CCounterTimer()
{
}
void CCounterTimer::Start(UINT nCount)
{
m_bRunning = TRUE;
m_nCount = nCount;
}
void CCounterTimer::Stop()
{
m_bRunning = FALSE;
m_nCount = 0;
}
void CCounterTimer::Step()
{
if (!m_bRunning || m_nCount == 0)
{
return;
}
m_nCount--;
if (m_nCount == 0)
{
Stop();
m_pListener->Update(this);
}
}
void CCounterTimer::AttachListener(CCounterTimerListener* pListener)
{
m_pListener = pListener;
(dynamic_cast<CDecisionMakingx*>(pListener))->m_vecTimer.push_back(this);
}
程序中的用法:
.h
///< 周期定时器处理
CCounterTimer m_timerStart, m_timerShootLine[ROBOTNUMBER], m_timerBallKick, m_timerEndLine;
vector<CCounterTimer*> m_vecTimer;
void Update(CCounterTimer* Timer);
.cpp
for (vector<CCounterTimer*>::iterator it = m_vecTimer.begin(); it != m_vecTimer.end(); ++it)
(*it)->Step();
……………………………………………………………………
m_timerStart.Start(20);
本人新手,望大家帮帮忙!!