Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

A bad workaround?

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!