Casting, Templates and References oh my.

Started by
3 comments, last by chad_420 18 years, 10 months ago
Im attempting to put together an engine based on enginuity articles here. To start I've gone through upto article 4 and accordfing to that should be able to build my engine with what was covered so far. One part is hanging me up. I have this function:

bool CKernel::AddTask(CMMPointer<ITask> &t)
{
  if(!t->Start())return false;

  //keep the order of priorities straight
  std::list< CMMPointer<ITask> >::iterator it;
  for(it=taskList.begin();it!=taskList.end();it++)
  {
    CMMPointer<ITask> &comp=(*it);
    if(comp->priority>t->priority)break;
  }
  taskList.insert(it,t);
  return true;
}


the problem Im having with it is as so: CKernel::GetSingleton().AddTask(CMMPointer<ITask>(soundTask)); wont compile giving me the error: D:\Src\engine\CApp.cpp no matching function for call to `CKernel::AddTask( CMMPointer<ITask>)' error D:\Src\engine\CKernel.h:33 candidates are: bool CKernel::AddTask(CMMPointer<ITask>&) So i tryed making it jsut take the variable not a reference and it compile, but then I get an access violation that I believe to be related. Any ideas? Has anyone else tryed to put together enginuity on dev-cpp/gcc? Edit more investigation has shown that the access violation is not related, but it still bugs me these errors, just making them not references seems like the wrong approach.
Advertisement
The problem is that the temporary value you're passing is considered constant, whereas your function takes a reference to a NON constant.

Since you're creating a copy of the task (or the CMMPointer<ITask> construct anyways, when you call insert), it looks like fixing your problem is simply a matter of changing:

bool CKernel::AddTask(CMMPointer<ITask> &t)

to:

bool CKernel::AddTask(const CMMPointer<ITask> &t)

Any access violation caused by this code would be a bug within CMMPointer, ITask, or whatever called this function (e.g. if it passed a bad argument).

HTH
Thanks MaulingMonkey, you nailed it dead on. Now time to hunt down this access violation, it appears to be in CMMPointer.
What you are looking for is actually fixing your ()
From making a temporary:
CKernel::GetSingleton().AddTask(CMMPointer<ITask>(soundTask));
To casting your pointer:
CKernel::GetSingleton().AddTask((CMMPointer<ITask>)soundTask);
Oh my I didn't catch that! I'm going to have to check that out thanks! Man that could definatly be the source of an access violation methinks.

This topic is closed to new replies.

Advertisement