Sign in to follow this  
ukdeveloper

A bad workaround?

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 this post


Link to post
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"
void
Paddle::someFunction()
{
// implementation
}

// events.cpp
#include "paddle.h"

Paddle myPaddle;

void
keyHandler()
{
myPaddle.someFunction();
}

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

Share this post


Link to post
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 this post


Link to post
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.

Thanks for your help.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Enigma
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


I fixed it.

In my header file, I had this:


class BRPaddle
{

public: // code

private: // 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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this