Trouble creating a Function

The Partridge Family were neither partridges nor a family. Discuss.
TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Trouble creating a Function

Post by TheCollector » August 16th, 2019, 7:36 pm

I'm on tutorial 6 of Chili's and he's creating a function in that one for DrawBox.

I followed it the way he did it exactly the only difference is I named my function DrawTree because it's drawing a tree not a box.

I'm getting an error when I declare the function: /********************************/
/* User Functions */
void DrawTree( int tx, int ty, int tr, int tg, int tb);
/********************************/
Error reads "Function definition cannot be found"

BUT... I have the function definition under ComposeFrame (which is part of the Game class I thought) as below:

void Game::DrawTree( int tx, int ty, int tr, int tg, int tb);
{
gfx.PutPixel(0, 0, 0, 255, 0);
gfx.PutPixel(-1, 0, 0, 255, 0);
gfx.PutPixel(-2, 0, 0, 255, 0);
gfx.PutPixel(-3, 0, 0, 255, 0);
gfx.PutPixel(-4, 0, 0, 255, 0);
gfx.PutPixel(-5, 0, 0, 255, 0);

gfx.PutPixel(0, +1, 0, 255, 0);
gfx.PutPixel(-1, +1, 0, 255, 0);
gfx.PutPixel(-2, +1, 0, 255, 0);
gfx.PutPixel(-3, +1, 0, 255, 0);
gfx.PutPixel(-4, +1, 0, 255, 0);
gfx.PutPixel(-5, +1, 0, 255, 0);

gfx.PutPixel(+1, 0, 0, 255, 0);
gfx.PutPixel(+2, 0, 0, 255, 0);
gfx.PutPixel(+3, 0, 0, 255, 0);
gfx.PutPixel(+4, 0, 0, 255, 0);
gfx.PutPixel(+5, 0, 0, 255, 0);

gfx.PutPixel(+1, +1, 0, 255, 0);
gfx.PutPixel(+2, +1, 0, 255, 0);
gfx.PutPixel(+3, +1, 0, 255, 0);
gfx.PutPixel(+4, +1, 0, 255, 0);
gfx.PutPixel(+5, +1, 0, 255, 0);

gfx.PutPixel(0, +2, 50, 0, 0);
gfx.PutPixel(0, +3, 50, 0, 0);
gfx.PutPixel(0, +4, 50, 0, 0);
gfx.PutPixel(0, +5, 50, 0, 0);
gfx.PutPixel(0, +6, 50, 0, 0);
gfx.PutPixel(0, +7, 50, 0, 0);
gfx.PutPixel(0, +8, 50, 0, 0);

gfx.PutPixel(-1, +2, 50, 0, 0);
gfx.PutPixel(-1, +3, 50, 0, 0);
gfx.PutPixel(-1, +4, 50, 0, 0);
gfx.PutPixel(-1, +5, 50, 0, 0);
gfx.PutPixel(-1, +6, 50, 0, 0);
gfx.PutPixel(-1, +7, 50, 0, 0);
gfx.PutPixel(-1, +8, 50, 0, 0);

gfx.PutPixel(+1, +2, 50, 0, 0);
gfx.PutPixel(+1, +3, 50, 0, 0);
gfx.PutPixel(+1, +4, 50, 0, 0);
gfx.PutPixel(+1, +5, 50, 0, 0);
gfx.PutPixel(+1, +6, 50, 0, 0);
gfx.PutPixel(+1, +7, 50, 0, 0);
gfx.PutPixel(+1, +8, 50, 0, 0);
}

and I'm also getting an error here that states "member function DrawTree may not be redeclared outside it's class"

What am I doing wrong?

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 16th, 2019, 7:43 pm

I'm also getting "unresovled external symbol "void_cdecl DrawTree(void)" (?DrawTree@@YAXXZ) referenced in function "private: void_thiscall Game::ComposeFram(void)" (?ComposeFrame@GAme@@AAEXXZ)"

and "1 unresolved externals" when I add the parameters to it. When I run it without the parameters these two errors don't come up.

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 16th, 2019, 7:57 pm

Okay, those last two errors were from the line of code where I tried to call the function into use. I removed that for now. Now I'm just getting "member function "Game::DrawTree" may not be redeclared outside it's class" and 'void Game::DrawTree(int,int,int,int,int)': redeclaration of member is not allowed

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

Re: Trouble creating a Function

Post by albinopapa » August 16th, 2019, 8:06 pm

Code: Select all

void Game::DrawTree( int tx, int ty, int tr, int tg, int tb);
{
   ...
}
Look at the end of that line of code there, you see it?
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

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 17th, 2019, 1:08 am

I wish I did but I don't. This is my first time creating a function.

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 17th, 2019, 1:11 am

I see that Chili isn't putting a semi-colon at the end of that first line of code you copy and pasted but when I don't put it in I get an error that it expected a ';'

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 17th, 2019, 1:29 am

Now I'm getting the member function Game::DrawTree may not be redeclared outside it's class, expected a ';', expected a declaration, 'Game::DrawTree': local function definitions are illegal.

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

Re: Trouble creating a Function

Post by albinopapa » August 17th, 2019, 6:40 am

When declaring a function, you must end with a semi-colon, this lets the compiler know this is a declaration and not a definition.

When you define a function, you do not use the semi-colon, and just define the function body, between the curly braces { }.

Member function declaration

Code: Select all

class Game
{
public:
private:
   void DrawTree( int tx, int ty, int tr, int tg, int tb);
};
Member function definition

Code: Select all

void Game::DrawTree( int tx, int ty, int tr, int tg, int tb)
{
     // draw code...
}
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

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

Re: Trouble creating a Function

Post by albinopapa » August 17th, 2019, 6:42 am

Game::DrawTree': local function definitions are illegal
This could mean you are trying to define the function inside another function, or you haven't declared the function in the Game class, but HAVE defined the function.
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

TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

Re: Trouble creating a Function

Post by TheCollector » August 17th, 2019, 6:23 pm

I definitely declared the function within the Game class (at least I think. I put it right where Chili did and it says it's under the Game class) but I decided to try putting my definition outside of ComposeFrame and it it will build like that without any errors so I was trying to define the function within another function (I guess the ComposeFrame function) which I can't do. Didn't know that. Thanks.

Post Reply