## Recommended Posts

Hi, I'm building Breakout, and am getting into the practice of keeping my source in separate files. I've got a file called paddle.h, which has the Paddle class for the Breakout game, and I have a file called events.cpp, which is a keyboard handler. The idea is that when a key is pressed, the function to handle it is in events.cpp, and it calls class members in the Paddle class to check if the paddle is still inside the game area, and move it accordingly if it is. I used to have paddle.cpp, which held the definitions of the class member functions, but that caused compiler errors due to multiple declaration (I do have inclusion guards - no worky [sad]). paddle.h was therefore included in both paddle.cpp and events.cpp, as events.cpp needed to be able to see the Paddle class in order to call the member functions. To solve the problem, I decided to put the Paddle class function definitions into the header file, eliminating paddle.cpp and putting it all in paddle.h. I know this is bad practice, but how acceptable is it? And can you think of a more acceptable alternative? I haven't tried, but I'd hazard a guess that doing away with paddle.h and putting it all in paddle.cpp wouldn't work either [oh] Any suggestions? Thanks in advance, ukdeveloper.

##### Share on other sites
Get it to work with two .cpp files. You're going to need to get used to this at some point: better sooner than later...

I assume your code looked sort of like the following (really cut down) code:
// paddle.h#ifndef _UKDEVELOPER__PADDLE_H_#define _UKDEVELOPER__PADDLE_H_class Paddle{    public:        void someFunction();}#endif
// paddle.cpp#include "paddle.h"voidPaddle::someFunction(){     // implementation}
// events.cpp#include "paddle.h"Paddle myPaddle;voidkeyHandler(){     myPaddle.someFunction();}

What compiler are you using? Which IDE (if any)?

##### Share on other sites
If the compiler gives problems about 'multiple declaration' it also tells you where the problems occur. Note that your .h should only include the things that you need in events.cpp, and not those that you need in paddle.cpp. So if you've got a global variable int x in paddle.cpp that you don't need to use directly in events.cpp, then don't put it in the header file paddle.h, but simply declare it in paddle.cpp.

##### Share on other sites
I'm using VS2005 Express Beta 2.

lucky_monkey, that's exactly how my code was before I hacked round it.

So are you saying using a class here is a bad idea? Could I create some other small function as an extern to act as a "bridge" between the two source files, meaning I only need to use the include once? That might work, I'm thinking.

##### Share on other sites
Exactly what errors were you getting? Were they compiler errors or linker errors? If your code looked like lucky_monkey's snippet (with the addition of a semi-colon at the end of the class definition) then you should not have been getting multiple declaration/definition errors.

Enigma

##### Share on other sites
Quote:
 Original post by EnigmaIf your code looked like lucky_monkey's snippet (with the addition of a semi-colon at the end of the class definition) then you should not have been getting multiple declaration/definition errors.Enigma

I fixed it.

class BRPaddle{public: // codeprivate: // code}Paddle; // Removing this and putting BRPaddle Paddle into my .cpp file solved the problem!

It was another of my silly mistakes, but thanks for all your help anyway!