# Returning a true in a function that has a bool return type

This topic is 1077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm honestly confused as to why this doesn't work any help would be greatly appreciated:

the call in main.cpp:

				if ((*_bagOfTetrominos.front()).checkCollision()) {
// Do Nothing
std::cout << "Did Nothing" << std::endl;
}
else {
std::cout << "Did Something" << std::endl;
(*_bagOfTetrominos.front()).setX((*_bagOfTetrominos.front()).getX() + BLOCK_SIZE);
}


Had the cout's just for debugging purposes:

and the checkCollision function :

bool T_Block::checkCollision()
{
for (int i = 0; i < _blockMapHeight; i++) {
for (int j = 0; j < _blockMapWidth; j++) {
if (blockMap[j][i] != 0) {

// Draw Individual Blocks
SDL_Rect checkPosition;

// Check Below Each Brick
checkPosition.x = _x + (i * BLOCK_SIZE);
checkPosition.y = _y + (j * BLOCK_SIZE);

Vector2D convertedCoords = convertCoords(checkPosition.x, checkPosition.y);

if (_gameBoard->_board[convertedCoords.getX()][convertedCoords.getY()] != 0) {
std::cout << "Collision Detected!" << std::endl;
return true;
}
}
}
}
return false;
}


When I watch the debugger it goes into the if statement showing that gameBoard coods != 0;

It even prints out "Collision Detected!" in the console but then it returns false? and prints out "Did Something".....

but then continues merrily down to return false.

Edited by Delite413

##### Share on other sites

Sorry to the people I was talking to in the chatroom, it crashed and now it says I'm unable to connect to the chatroom please contact an administrator.

##### Share on other sites

Sounds suspiciously what might happen if you write out of bounds somewhere (and probably somewhere else).

Are the convertedCoords always in bounds of _board?

##### Share on other sites

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness.

This is usually the first thing you should do when you encounter something that doesn't make sense. The other thing you can do is look at the assembly code that is being executed and see if it doesn't jump to the wrong location.

##### Share on other sites
Debugging per line "step into" should take you through the code per line. So you can see if the values are as expected and why true or false is returned. You could start the debugging by a break point in the main code, where you call the collision function.

##### Share on other sites

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness.

This happens a lot; especially under Windows with Visual Studio.  Do a clean and rebuild.

##### Share on other sites
It's doing exactly what you told it to do.

if ((*_bagOfTetrominos.front()).checkCollision())
This code is obviously printing "Did Nothing" when checkCollision returns true.

**Update**: I may have misread the problem. Maybe still too early for my brain, but am I understanding what the problem is properly? Edited by SeanMiddleditch

##### Share on other sites

How about you print a  seperator in between consecutive executions of the if statement that should help too.

##### Share on other sites

Sorry for the late reply guys, long day, just now finally able to sit down at my PC >_<

Sounds suspiciously what might happen if you write out of bounds somewhere (and probably somewhere else).

Are the convertedCoords always in bounds of _board?

Checking the values in the debugger it never goes out of bounds.

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness.

That worked! I apologize I'm still new so can you clarify this statement for me cause I'm sorta lost. Is it just that what's being run isn't what I'm seeing on my screen? What exactly is it running instead? Just asking for my knowledge, thank you.

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness.

This is usually the first thing you should do when you encounter something that doesn't make sense. The other thing you can do is look at the assembly code that is being executed and see if it doesn't jump to the wrong location.

As noted above this solved it, thank you!

Debugging per line "step into" should take you through the code per line. So you can see if the values are as expected and why true or false is returned. You could start the debugging by a break point in the main code, where you call the collision function.

Yeah even debugging, I would see it enter the if statement within the checkCollision method, go right to the return true, and like completely disregard it, just go outside of the if statement block, and go down the "}" until it got to the return false; then exit the method.

Depending on your environment another possibility is that you have a situation where the source code doesn't actually match the object code you're debugging. In that case a full rebuild may eliminate some weirdness.

This happens a lot; especially under Windows with Visual Studio.  Do a clean and rebuild.

This solved it thank you sir ;) and indeed I'm using Visual Studio.

It's doing exactly what you told it to do.

if ((*_bagOfTetrominos.front()).checkCollision())
This code is obviously printing "Did Nothing" when checkCollision returns true.

**Update**: I may have misread the problem. Maybe still too early for my brain, but am I understanding what the problem is properly?

It was printing  "Did Something" which confused the hell outa me lol. It's all good, mornings do that to me too.

How about you print a  seperator in between consecutive executions of the if statement that should help too.

The above solved it, but I definitely do appreciate your input.Thank you.

##### Share on other sites

I'm actually not seeing that in Visual Studio too much, though there is probably one rule of thumb to follow: never change and save a source file while compiling. That's the one case I found where you often end up with the old version getting compiled and the new version being considered "already up to date" on subsequent builds.

##### Share on other sites

What exactly is it running instead?

Code you wrote in the past.

L. Spiro

##### Share on other sites

Learning exercise: What happens here when bagOfTetriminos is empty?

if ((*_bagOfTetrominos.front()).checkCollision())

... nothing good, that's for sure.

##### Share on other sites

Learning exercise: What happens here when bagOfTetriminos is empty?

if ((*_bagOfTetrominos.front()).checkCollision())

... nothing good, that's for sure.

Thankfully that situation has arrived, but you are correct I have been failing in terms of error checking. Appreciate the reminder ;) Also have since converted to " -> " looking back at that makes me sad.

##### Share on other sites

One thing I do is "sanity check" to confirm what part of the code is having the problem.

In this case, I would've done something like this:

std::cout << "checkCollision() result: " << (aSingleTetromino.checkCollision()? "true" : "false") << std::endl;


Note that, not only is it possible that the compiled code you are running isn't the written code that you are reading, but it's also possible that, when stepping through with a debugger, the debugger can show you the wrong values. It makes programming extra super duper fun!

This happens a lot; especially under Windows with Visual Studio.  Do a clean and rebuild.

With MinGW also! Microsoft's compiler doesn't have a monopoly on bugs.