Public Group

# [SDL] Spliting my code into many *.h and *.cpp files problems.

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

## Recommended Posts

Im writing a pong clone (again) with as much OOP as i can. right now i want to split my code (which i just started doing) into .h and .cpp files. Right now i have 4 files: paddles.h, paddles.cpp, data.h, main.cpp. Ive written a paddle class in the paddle.h file, ive written the definitions for that classes functions in paddles.cpp and i included paddles.h in it. My data.h file has the definitions of image drawing functions. Now i include data.h and then the paddles.cpp into the main.cpp, but it saiz that in paddles.cpp the image drawing functions are undeclared identifiers. What am i doing wrong? And i hope somene will be able to understand what ive wanted to say... ------
                     data.h ---|
|---> main.cpp



##### Share on other sites
Show me the actual includes you are putting in your files and I should be able to help you.

##### Share on other sites
For header files (*.h), there is a format you must follow or you will have redfinition / not defined problems:

#ifndef PUT_SOMETHING_HERE
#define PUT_SOMETHING_HERE

//all your class definitions and function prototypes

#endif

include paddles.h and data.h if needed

For main.cpp:

##### Share on other sites
Consider each .cpp file as a separate entity during compiling. One .cpp file doesn't know about or care about any other .cpp file while compiling.

So when you hit compile, here's what happens: paddles.cpp is compiled into paddles.obj (or some other similar file). It needs to know the format of functions to call, available variables that it can get at, etc., but it doesn't need to know about the internals of any functions, except for it's own functions. Then, main.cpp is compiled into main.obj. Similarly, it only needs to know how to call functions, the type and name of available variables, and it's own internal code. Finally, the linker gets involved, and links the multiple .obj files together. The linker is what puts it all together, so that function calls that previously had the correct format, but didn't have access to the actual code, the implementation, now do have that access.

Probably not the best explanation, but hopefully it helps.

##### Share on other sites
Hummmm... Ive got 37 error. Few of them:

syntax error : missing ';' before '*' (in "SDL_Surface *screen;")
'SDL_Surface' : missing storage-class or type specifiers
'screen' : missing storage-class or type specifiers

And many of the problems are connected to the:
SDL_Surface *screen;
SDL_Surface *back;
lines.
Right now they are on top of my data.h, but where am i supposed to put them?

##### Share on other sites
Ok, ive fixed all of that, but now im back to where i was at the start, 3 error messages:

'DrawImg' : undeclared identifier

It doesnt recognize the image drawing function and the SDL surfaces in the paddles.cpp, i think ive done everything right.

##### Share on other sites
Damn, im sorry for a triple post, but i need this to be noticed.
The three errors come from 1 function:
void paddles::draw() {	DrawImg(Cpaddle, cpaddle.xpos, cpaddle.ypos, 0, 0, 0, 0);	DrawImg(Ppaddle, ppaddle.xpos, ppaddle.ypos, 0, 0, 0, 0);}

When i try to use the function ("paddles.draw();") in the main game loop i get these aditional errors:
'.draw' must have class/struct/union type

struct paddles {	::paddles();	int xpos, ypos;	void draw();	}cpaddle, ppaddle;

##### Share on other sites
Ill bump this once before letting it die, maybe someone will be able o help.
And a rating++ to Agony and mrbreakit for help.

##### Share on other sites

If you do that in your source too, it could be part of the problem.
Symbolic names in c++ are case-sensitive, you know.

You also said you tried "paddles.draw()", but that could never work. "paddles" is the classname and "draw" is a nonstatic method which need an instance of the class to be called on. Maybe you need to brush up on the concepts of class and objects in c++.

##### Share on other sites

Noticed some more. If DrawImg() is defined in data.h, you need to include it in paddles.cpp. Which you might already be doing, but I have some trouble telling from your description. Post all three files in source-tags, that'll be quicker and easier for both you and people responding.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 13
• 46