I am having an issue with SDL and C++ classes. Basically, I want my Player to have a class of its own, so I made this (Player.cpp):
#include <SDL/SDL.h>
class Player {
int x;
int y;
};
I've got my main file, with the main () function, and the drawing functions. I wanted to call something like "Player player = new Player ();" (Java, Python and other languages allow me to do something like this).
I guessed that I would have to compile Player.cpp first, but it complains about not having a main function and whatnot.
So, I have a few questions:
- How can I have my own SDL Player class, and associate it with one or more sprites;
- I heard that I need header files to link the files, but I have no idea how;
- How to compile the different files, I am using a Makefile to build the game btw;
Thank you tons in advance.
Oh, and regarding question #1, I would also like to know how I can call the Player class from the main file so that then I can draw it.
First of all, I would advise you read this article. Essentially, the class definition should be in a header file (e.g. Player.h). If the class requires source code, then you need an accompanying source file (e.g. Player.cpp). To use the class in a third file (e.g. Main.cpp), you need to #include the header file in this third file.
In C++, one would use "Player player;". Dynamic allocation (i.e. new/delete) is not necessary here.
Can you post your make file? It shouldn't be trying to compile and link Player.cpp on its own, it should be trying to compile Main.cpp and Player.cpp, and then link them together to produce your executable.
Hi munchor,
Your doubts are basically C++ doubts instead of SDL related.
I'd totally consider studying C++ and Object Oriented Programming fist before really geting into SDL, OpenGL and etc.
Don't get me wrong, but I'm saying that because I was just like you, and could only really get things on my own when I decided to learn this topics.
If you already program, it's going to be really fast to learn, and I recommend the book "Sams teach yourself c plus plus in one hour a day". Really helped me to study by myself.
First of all, I would advise you read this article. Essentially, the class definition should be in a header file (e.g. Player.h). If the class requires source code, then you need an accompanying source file (e.g. Player.cpp). To use the class in a third file (e.g. Main.cpp), you need to #include the header file in this third file.
In C++, one would use "Player player;". Dynamic allocation (i.e. new/delete) is not necessary here.
Can you post your make file? It shouldn't be trying to compile and link Player.cpp on its own, it should be trying to compile Main.cpp and Player.cpp, and then link them together to produce your executable.
I will read that article you sent me and will definitely change it, and based on the way the article looks like, it'll teach me everything I need to know, thank you so much, I've been looking for something like this for a few months now.
Hi munchor,
Your doubts are basically C++ doubts instead of SDL related.
I'd totally consider studying C++ and Object Oriented Programming fist before really geting into SDL, OpenGL and etc.
Don't get me wrong, but I'm saying that because I was just like you, and could only really get things on my own when I decided to learn this topics.
If you already program, it's going to be really fast to learn, and I recommend the book "Sams teach yourself c plus plus in one hour a day". Really helped me to study by myself.
[]'s
Hi returnONE,
I understand exactly what you're saying. I don't need to study OOP, because, well, I know OOP already, I just happened to learn with implementations on other languages (Python, JS, Java, etc.). What I need to study, is how C++ handles classes and multiple files, and rip off gave me just the thing
This depends on the compiler you've chosen to use, additionally if you're using an IDE or not.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
If you read the error (instead of just pasting it) you will see that its telling you that you defined the constructor twice. Which you did:
Player() {};
Player::Player (int player_x, int player_y) {
x = player_x;
y = player_y;
}
Furthermore, the signature of those constructors don't match. Lose the {} in the header, and add the parameters you're passing as well.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
...
well, first off, seperate compilation from linking.
so compile game.cpp the same way you do player.cpp
then link them: g++ game.o player.o -o game
This is also why you should use a basic makefile, because you can simply add a couple of lines (or none if you use wildcards) and it'll take care of it.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.