Trying to use functions

Started by
16 comments, last by In_Yack_Mode 15 years, 4 months ago
You're doing this:

movement.h
extern int man_x=0;


You can't initialise (assign the zero) to the extern. You need to create a movement.cpp and do this:

movement.h
extern int man_x;


movement.cpp
int man_x=0;


Basically, the extern int man_x; tells the compiler "There is an int called man_x defined somewhere else, possibly in another translation unit, let the linker worry about it."

You can't assign a value to this declaration as you are not actually defining the variable here.

So you need to actually define the variable somewhere else and by not doing that in the header, it means you can include the header multiple times in other translation units without each one getting a copy of the variable.
Advertisement
I did that before I posted the error last night but I was getting this:
[Linker error] undefined reference to `man_x'
[Linker error] undefined reference to `man_y'

I figured that wasn't it and put it back like it was. I usually get those errors when I forget to include something but I have it included.
Important.
What in the world is "END_OF_FUNCTION(check_keys)" supposed to do? All I could imagine is that it would evaluate to nothing, and just be a marker for the end of a function... but is that really necessary? Just use a comment if you really want something telling you what the closing bracket is for.
Quote:Original post by Ezbez
What in the world is "END_OF_FUNCTION(check_keys)" supposed to do? All I could imagine is that it would evaluate to nothing, and just be a marker for the end of a function... but is that really necessary? Just use a comment if you really want something telling you what the closing bracket is for.


I was reading someone else's function they built and they had it so I thought that it was needed. I just removed it now.

rip-off I read that and might have missed what you were trying to point out so I'm rereading it.
Quote:Original post by In_Yack_Mode
I did that before I posted the error last night but I was getting this:
[Linker error] undefined reference to `man_x'
[Linker error] undefined reference to `man_y'

I figured that wasn't it and put it back like it was. I usually get those errors when I forget to include something but I have it included.


you're getting that because whatever source file you're using the variable in doesn't know about it.

try this

movement.h
extern int man_x;extern int man_y;


movement.cpp
void SetPosition(void){    man_x = 0;    man_y = 0;}


main.cpp
#include "movement.h" // this will let main.cpp know everything about movement.hvoid main(void){    SetPosition(); // initialize man_x and man_y    // you should be able to use the variables as you need now.}











[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Quote:Original post by In_Yack_Mode
I was reading someone else's function they built and they had it so I thought that it was needed. I just removed it now.

rip-off I read that and might have missed what you were trying to point out so I'm rereading it.


... Sigh.

Programming is a mental discipline, it requires thinking. What rip-off was trying to point out was the entire article; you have to understand how these things work in order to do them properly. Putting things in because you "think they are needed" - because you saw them somewhere else - is a one-way ticket to nowhere as a programmer. Try not to do anything without at least a basic understanding of what you are doing. Try writing things in more basic ways, too, and worry about the language fundamentals before flashy stuff. Understanding how to write and call functions, and organize your source code files, is much more important than figuring out how to process the arrow keys with Allegro (I'm guessing wildly about Allegro, based on the results of Google searching for END_OF_FUNCTION).
Quote:Original post by Zahlman
... Sigh.

Programming is a mental discipline, it requires thinking. What rip-off was trying to point out was the entire article; you have to understand how these things work in order to do them properly. Putting things in because you "think they are needed" - because you saw them somewhere else - is a one-way ticket to nowhere as a programmer. Try not to do anything without at least a basic understanding of what you are doing. Try writing things in more basic ways, too, and worry about the language fundamentals before flashy stuff. Understanding how to write and call functions, and organize your source code files, is much more important than figuring out how to process the arrow keys with Allegro(I'm guessing wildly about Allegro, based on the results of Google searching for END_OF_FUNCTION).


I think I am going to take your advice. Thanks everybody for your help on this though. freeworld your code got it working, but Zahlman is right I need to understand more basic stuff before jumping too Allegro.


This topic is closed to new replies.

Advertisement