Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ship *player = static_cast<Ship *>(a == player_ ? a : (b == player_ ? b : 0));
C++




I know what static_cast is but I don't know what's done with the conditional operators.

First-chance exception at 0x00DDD250 in AsteroidsTest_2012.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6.


I just need hints about what might be wrong. Not direct answers. I'll find the answer on my own. I just need the hint.

Btw the crash takes me here: -

void Collision::DoCollisions(Game *game) const
{
	for (ColliderList::const_iterator colliderAIt = colliders_.begin(), end = colliders_.end();
		colliderAIt != end;
		++colliderAIt)
	{
		ColliderList::const_iterator colliderBIt = colliderAIt;
		for (++colliderBIt; colliderBIt != end; ++colliderBIt)
		{
			Collider *colliderA = *colliderAIt;
			Collider *colliderB = *colliderBIt;
			if (CollisionTest(colliderA, colliderB))
			{
				game->DoCollision(colliderA->entity, colliderB->entity);
			}
		}
	}
}


What I have tried:

Tried rectifying the problem but I land on this code
Posted
Updated 19-Sep-18 0:26am

1 solution

Look at it: it's just a nested conditional.
a == player_ ? a : (b == player_ ? b : 0)

If a == player_ then it returns a.
Otherwise if b == player_ then it returns b.
If both tests fail it returns 0

Think of it as an "in place if":
if (a == player_)
   return a;
else if (b == player_)
   return b;
return 0;
 
Share this answer
 
Comments
Siddhant Bhati 19-Sep-18 6:54am    
Oh thanks man.

Can I ask you more doubts about this code? I just want you to give me hints about what I should go for.

So this is an asteroid type game and when the player or bullet collides with the asteroid the game crashes and takes me here :-

void Collision::DoCollisions(Game *game) const
{
for (ColliderList::const_iterator colliderAIt = colliders_.begin(), end = colliders_.end();
colliderAIt != end;
++colliderAIt)
{
ColliderList::const_iterator colliderBIt = colliderAIt;
for (++colliderBIt; colliderBIt != end; ++colliderBIt)
{
Collider *colliderA = *colliderAIt;
Collider *colliderB = *colliderBIt;
if (CollisionTest(colliderA, colliderB))
{
game->DoCollision(colliderA->entity, colliderB->entity);
}
}
}
}


First-chance exception at 0x00CFD250 in AsteroidsTest_2012.exe: 0xC0000005: Access violation reading location 0xFEEEFEF6.
OriginalGriff 19-Sep-18 7:05am    
You really should ask that as a new question, but at a guess - and I can't run your code so that's all it can be - one of your pointers isn't right.

Use the debugger to examine what is happening while you code runs and that should help you find out what pointer is bad, and that should help you find why it's bad!
Siddhant Bhati 19-Sep-18 8:59am    
Hey Thanks,

i think in this for (++colliderBIt; colliderBIt != end; ++colliderBIt)
{
Collider *colliderA = *colliderAIt;
Collider *colliderB = *colliderBIt;
if (CollisionTest(colliderA, colliderB))
{
game->DoCollision(colliderA->entity, colliderB->entity);
}
}

ColliderA wouldn't be receiving values or getting any memory allocated. Right? or Everytime it might be getting the same value of ColliderAIt.

This might be a problem?
OriginalGriff 19-Sep-18 10:00am    
Check with the debugger - it will show you exactly what is in what variable, and it's the only way to be sure.
You assign a value to colliderA (from colliderAlt) but I have no way of knowing what the heck that contains! :laugh:

Seriously, you need to use the debugger - it's the easiest way to find out instead of guessing.
Siddhant Bhati 20-Sep-18 1:33am    
It says unable to read memory for radius, entity and enabled. Possibly they are empty that's why

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900