PDA

View Full Version : Functions not working



Xero_Tolerance
05-30-2002, 10:42 AM
I have a rather big project that I'm working on, terrain engine... if you wanna know more about it check it out at http://members.cox.net/evilremy but that's not the point. The thing is I have like 10 cpp files and there is a main one with all the opengl drawing functions and what not. So, I have classes for all of them, like a menu class, a build class, a player class. If I call the class from the main file, like if i call a function from that main file it works fine. But, if I call a function from one of the other files it won't run the function if it's not in the same class. I have no idea what the problem is. If I call the function from within a certain class to another class in the main file it works fine too, so that means it's just a file problem, nothing about the classes. If anyone knows what the problem may be I would appreciate it greatly, thanks.

littledevore
05-30-2002, 11:36 AM
I don't know if this is the best solution, but I got it to work. First, create a header file for your main cpp file. Include all of your functions in the cpp file in the header. Now you just #include<header.h> in your other cpp files that you want to call functions from the main program. Then it should recognize the functions. I hope this helps.

Xero_Tolerance
05-30-2002, 02:23 PM
Well, i have separate headers for each of the different cpp files, so putting them all in one header wouldn't be ideal unless I want a whole lot of code in one file, which I don't find to be very easy to move around in. Ummm, it's not like I get any errors or anything, I mean it runs and everything, just whenever I call the function... such as pPlayer.Move(x,y) or what not, it won't work unless I'm calling it in the main file. I think it may have to do with the fact that I declared the class for the first time in there, like that is where I have all the... Player pPlayer; type things, I forget what they're called. Anyway, anyone got any other ideas?

Deiussum
05-31-2002, 04:39 AM
What littledevore was suggesting is that for all the functions in your main file, put the prototypes of those functions in a separate header. Then in the .cpp files for any of the classes that need to use those functions, you #include that header.

If you have a class that needs access to another class, you also have to #include the header to that other class.

If you had a better understanding of C++, you would see why this has to be. Each .cpp file is compiled separately, from top to bottom. Before a function can be used it has to be defined or declared in the scope of that file, either directly or in an included file. It's only after all the .cpp files are compiled that they are linked together into a single binary.