Page 1 of 2

Trouble creating a Function

Posted: August 16th, 2019, 7:36 pm
by TheCollector
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?

Re: Trouble creating a Function

Posted: August 16th, 2019, 7:43 pm
by TheCollector
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.

Re: Trouble creating a Function

Posted: August 16th, 2019, 7:57 pm
by TheCollector
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

Re: Trouble creating a Function

Posted: August 16th, 2019, 8:06 pm
by albinopapa

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?

Re: Trouble creating a Function

Posted: August 17th, 2019, 1:08 am
by TheCollector
I wish I did but I don't. This is my first time creating a function.

Re: Trouble creating a Function

Posted: August 17th, 2019, 1:11 am
by TheCollector
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 ';'

Re: Trouble creating a Function

Posted: August 17th, 2019, 1:29 am
by TheCollector
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.

Re: Trouble creating a Function

Posted: August 17th, 2019, 6:40 am
by albinopapa
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...
}

Re: Trouble creating a Function

Posted: August 17th, 2019, 6:42 am
by albinopapa
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.

Re: Trouble creating a Function

Posted: August 17th, 2019, 6:23 pm
by TheCollector
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.