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

Started by
62 comments, last by Spudder 19 years, 6 months ago
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
paddles.h ----> paddles.cpp ---|

data.h- drawing functions paddles.h- paddle class paddles.cpp- paddles class function definitions (one of them is drawing of the paddle using a function described in data.h)
Advertisement
Show me the actual includes you are putting in your files and I should be able to help you.
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

For paddles.cpp:

include paddles.h and data.h if needed

For main.cpp:

include paddles.h, data.h
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.

Your paddles.cpp, when compiling, needs to know about paddles.h, and, if it does drawing stuff, data.h as well. Thus, paddles.cpp needs to #include both paddle.h and data.h.

Your main.cpp needs to know about paddles.h and data.h as well (assuming it also does drawing stuff). It doesn't care about paddles.cpp, because it doesn't care about how paddles do their thing; all it cares about is how to tell paddles to do their thing. So in main.cpp you include paddles.h and data.h.

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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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;
SDL_Surface *Cpaddle;
SDL_Surface *Ppaddle;
lines.
Right now they are on top of my data.h, but where am i supposed to put them?
Ok, ive fixed all of that, but now im back to where i was at the start, 3 error messages:

'DrawImg' : undeclared identifier
'Cpaddle' : undeclared identifier
'Ppaddle' : undeclared identifier

It doesnt recognize the image drawing function and the SDL surfaces in the paddles.cpp, i think ive done everything right.
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);}

(doesn't recognize DrawImg, Cpaddle, Ppaddle)
When i try to use the function ("paddles.draw();") in the main game loop i get these aditional errors:
'paddles' : undeclared identifier
'.draw' must have class/struct/union type

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


Using ppaddle.draw(); or cpaddle.draw(); doesn't help.
Ill bump this once before letting it die, maybe someone will be able o help.
And a rating++ to Agony and mrbreakit for help.

I notice you sometimes write either "cpaddle" or "Cpaddle".
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++.

I was the AC above, forgot to log in.

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.
---------"It''s always useful when you face an enemy prepared to die for his country. That means both of you have exactly the same aim in mind." -Terry Pratchett

This topic is closed to new replies.

Advertisement