Beginner CPP Homework 13

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
CKatt
Posts: 15
Joined: March 2nd, 2018, 8:37 pm

Beginner CPP Homework 13

Post by CKatt » March 23rd, 2018, 12:55 am

I've been playing around with the code from tutorial 13 and what i am trying to do is get the meter to work for the poos instead of the goal.

I'm trying to add the meter level increase to the ProcessConsumption function so with each poo eaten the meter increases. the first problem was that i couldn't get access to the increase level function

Code: Select all

void Poo::ProcessConsumption( const Dude& dude,  Meter& meter) //////////added parameter here
{
	const int duderight = dude.GetX() + dude.GetWidth();
	const int dudebottom = dude.GetY() + dude.GetHeight();
	const int pooright = x + width;
	const int poobottom = y + height;

	if( duderight >= x &&
		dude.GetX() <= pooright &&
		dudebottom >= y &&
		dude.GetY() <= poobottom )
	{
		isEaten = true;
		meter.increaseLevel(); ////////////////////now i can call the function here
	}
}
What I came up with is adding a reference to the meter in the parameters of the process consumption function. That seemed to work but now when i call the function in Game.CPP the IDE is not happy. It doesn't seem to like the added parameter. telling me 'meter is unidentified' i have included Meter.h but I guess that's not enough.

Code: Select all


		poo0.ProcessConsumption( dude,  meter); ////////// Visual Studio don't like me when i put meter here
maybe i'm going about this all wrong i only started with CPP a few weeks ago so there is still a lot I don't know. If anybody can explain why this doesn't work and what i could do instead I would be very grateful.

Thanks

Firepath
Posts: 77
Joined: March 10th, 2018, 11:53 pm

Re: Beginner CPP Homework 13

Post by Firepath » March 25th, 2018, 5:02 am

CKatt wrote:I've been playing around with the code from tutorial 13 and what i am trying to do is get the meter to work for the poos instead of the goal.

I'm trying to add the meter level increase to the ProcessConsumption function so with each poo eaten the meter increases. the first problem was that i couldn't get access to the increase level function

Code: Select all

void Poo::ProcessConsumption( const Dude& dude,  Meter& meter) //////////added parameter here
{
	const int duderight = dude.GetX() + dude.GetWidth();
	const int dudebottom = dude.GetY() + dude.GetHeight();
	const int pooright = x + width;
	const int poobottom = y + height;

	if( duderight >= x &&
		dude.GetX() <= pooright &&
		dudebottom >= y &&
		dude.GetY() <= poobottom )
	{
		isEaten = true;
		meter.increaseLevel(); ////////////////////now i can call the function here
	}
}
What I came up with is adding a reference to the meter in the parameters of the process consumption function. That seemed to work but now when i call the function in Game.CPP the IDE is not happy. It doesn't seem to like the added parameter. telling me 'meter is unidentified' i have included Meter.h but I guess that's not enough.

Code: Select all


		poo0.ProcessConsumption( dude,  meter); ////////// Visual Studio don't like me when i put meter here
maybe i'm going about this all wrong i only started with CPP a few weeks ago so there is still a lot I don't know. If anybody can explain why this doesn't work and what i could do instead I would be very grateful.

Thanks
I'll have a go at helping.

Firstly did you change the declaration of ProcessConsumption to be like the definition (with the Meter reference)?

Next I'd look at where you're calling:

Code: Select all


		poo0.ProcessConsumption( dude,  meter); ////////// Visual Studio don't like me when i put meter here
Do you have a variable called meter available in that scope or globally (memberly?) ?


Googling helps a lot of the time and I know C++ can give you some heinous error messages that seem to not have anything to do with the actual problem.

Keep on learning! This is about my fourth decent attempt at learning C++ throughout the last ten plus years. It gets a little easier and makes more sense as you learn.


Also I just read that you're trying to use it for the Poos instead of the Goal. I haven't looked at the code, but if Meter references Poo in any way you might have trouble building due to circular references. You would need to forward-declare one of the classes to get it to work. Forward declaration is something Chili goes into in about beginner 20 to 23 or so.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Beginner CPP Homework 13

Post by albinopapa » March 25th, 2018, 7:53 am

Also I just read that you're trying to use it for the Poos instead of the Goal. I haven't looked at the code, but if Meter references Poo in any way you might have trouble building due to circular references. You would need to forward-declare one of the classes to get it to work. Forward declaration is something Chili goes into in about beginner 20 to 23 or so.
I'd wager this is the case here. If Poo.h #include "Meter.h" and Meter.h #include "Poo.h" then this is the circular dependency that Firepath refers to. As long as the definition of ProcessConsumption is in a .cpp file, you can forward declare class Meter; in Poo.h and #include "Meter.h" in Poo.cpp. This will eliminate the circular dependency.

Poo.h

Code: Select all

class Meter;

class Poo
{
public:
     void ProcessConsumption( Dude& dude, Meter& meter );
};
Poo.cpp

Code: Select all

#include "Poo.h"
#include "Meter.h"
void Poo::ProcessConsumption( Dude& dude, Meter& meter)
{
     // code goes here
}
Meter.h

Code: Select all

#include "Poo.h"
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

CKatt
Posts: 15
Joined: March 2nd, 2018, 8:37 pm

Re: Beginner CPP Homework 13

Post by CKatt » April 3rd, 2018, 5:00 pm

Thanks for the help guys.
Yeah, looks like I had a circular dependency issue. I seem to have it sorted now.
Cheers!

User avatar
bobloblaw
Posts: 100
Joined: April 15th, 2013, 3:05 am
Location: Earth, USA, California, SF Bay Area
Contact:

Re: Beginner CPP Homework 13

Post by bobloblaw » April 26th, 2018, 7:56 pm

In order to avoid circular dependency I will often have the function return a bool.

In game.cpp call the proccessConsumption how you like.

Code: Select all

if( poo01.ProcessConsumption( dude ) )
{
     meter.increaseLevel();
}

bool Poo::ProcessConsumption( const Dude& dude )
{
   const int duderight = dude.GetX() + dude.GetWidth();
   const int dudebottom = dude.GetY() + dude.GetHeight();
   const int pooright = x + width;
   const int poobottom = y + height;

   if( duderight >= x &&
      dude.GetX() <= pooright &&
      dudebottom >= y &&
      dude.GetY() <= poobottom )
   {
        isEaten = true;
        return true;
   }
   else
   {
        return false;
   }
}
That way game.cpp knows about Meter and Poo but they don't have to know about each other.
Just one solution to the issue if I understood your desire properly.

~cheers
Here for your Artistic needs, Pixel or Vector I love it all. Check it out - Click Me

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Beginner CPP Homework 13

Post by albinopapa » April 27th, 2018, 4:38 am

@Bob: I usually prefer something similar to this as well. In this scenario, why is it the poo's responsibility to increase the meter? Seems like it would be the Game's responsibility. That's what goes through my head anyway. Returning a bool from a function named ProcessConsumption though when really you have turned it into a HasCollided function, I struggle with. If a function named ProcessConsumption returned false, I would have to ask: "did the function fail to process?, was there an error I should be checking for?, or did it just fail to process consumption?".

Code: Select all

bool Poo::HasCollided( const Dude& dude )
{
     const int duderight = dude.GetX() + dude.GetWidth();
     const int dudebottom = dude.GetY() + dude.GetHeight();
     const int pooright = x + width;
     const int poobottom = y + height;

     return ( duderight >= x && dude.GetX() <= pooright &&
                 dudebottom >= y && dude.GetY() <= poobottom );
}

void Game::UpdateModel()
{
     if( pool.HasCollided( dude ) )
     {
          meter.increaseLevel();
     }
}

// Or even less coupling would be to define a Rect struct and define an overlaps(const Rect& left, const Rect& right ) function.
struct RectI
{
    int left,top,right,bottom;
};

bool Overlaps( const RectI& A, const RectI& B )
{
    return ( ( A.left < B.right ) && ( A.right > B.left ) ) &&
              ( ( A.top < B.bottom ) && ( A.bottom > B.top ) );
}

RectI Poo::GetRect()const
{
    return {x, y, x + width, y + height};
}
RectI Dude::GetRect()const
{
    return {x, y, x + width, y + height};
}

void Game::UpdateModel()
{
    if( Overlaps( poo.GetRect(), dude.GetRect() )
    {
        meter.increaseLevel();
    }
}
This way, Poo doesn't even know about Dude and Dude doesn't know about Meter. Poo and Dude need to know about RectI, Game will know about RectI since Poo and Dude will most likely have #include "Rect.h" in the header files.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply