Trouble creating a Function
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Trouble creating a Function
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?
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?
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
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.
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.
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
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
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Trouble creating a Function
Code: Select all
void Game::DrawTree( int tx, int ty, int tr, int tg, int tb);
{
...
}
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
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
I wish I did but I don't. This is my first time creating a function.
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
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 ';'
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
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.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Trouble creating a Function
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
Member function 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);
};
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
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Trouble creating a Function
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.Game::DrawTree': local function definitions are illegal
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
-
- Posts: 41
- Joined: August 1st, 2019, 9:57 pm
Re: Trouble creating a Function
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.