Jump to content
  • Advertisement
Sign in to follow this  

[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.

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

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
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)

Share this post

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


//all your class definitions and function prototypes


For paddles.cpp:

include paddles.h and data.h if needed

For main.cpp:

include paddles.h, data.h

Share this post

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

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.

Share this post

Link to post
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;
SDL_Surface *Cpaddle;
SDL_Surface *Ppaddle;
Right now they are on top of my data.h, but where am i supposed to put them?

Share this post

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

Share this post

Link to post
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);

(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 {
int xpos, ypos;
void draw();

}cpaddle, ppaddle;

Using ppaddle.draw(); or cpaddle.draw(); doesn't help.

Share this post

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

Link to post
Share on other sites
Guest Anonymous Poster

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++.

Share this post

Link to post
Share on other sites

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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!