SFML community forums
Help => General => Topic started by: Xenavar on February 19, 2010, 03:57:31 am
-
I am having trouble returning a pointer in my class..
Caracter.H
class TCaracter
{
private:
sf::Sprite Sprite;
struct Move *Moule_move;
struct Move *Stand;
struct Move *Courant_move;
struct Move *Debut_move;
public:
struct Move Get_MovePointer(string);
}
Caracter.Cpp
struct Move TPersonnage::Get_MovePointer()
{
return *Courant_move;
}
Main.cpp
TCaracter Player1;
if (Player1.Get_MovePointer() == NULL)
{
}
The bolded sentence gives me an errow saying : Main.cpp|19|error: request for member `Get_MovePointer' in `Perso', which is of non-class type `TPersonnage*'|
Guessing its in the syntax. Any help?
-
Your function declaration in the class TCaracter has the following signature:
struct Move Get_MovePointer(string);
But the definition doesn't match, since it belongs to a different class TPersonnage and uses no parameters.struct Move TPersonnage::Get_MovePointer()
Besides, writing struct in declarations is quite unusual in C++. You can once forward-declare Move, and then forget about the struct identifier.
-
I removed string, but it still doesn't work :?
-
I removed string, but it still doesn't work :?
As stated, the class is still wrong. Once you use TCaracter and once TPersonnage. ;)
-
And you're missing a ; at end of your TCharacter header
-
Lol, let's start this again.. I translated my variables to english (they were french) for you guys to understand better but I left some of them out so it just didn't make sense anymore.. this is my real code, with the same error:
Caracter.H
class TCaracter
{
private:
sf::Sprite Sprite;
struct Move *Moule_move;
struct Move *Stand;
struct Move *Courant_move;
struct Move *Debut_move;
public:
struct Move Get_MovePointer();
} ;
Caracter.Cpp
struct Move TCaracter::Get_MovePointer()
{
return *Courant_move;
}
Main.cpp
TCaracter Player1;
if (Player1.Get_MovePointer() == NULL)
{
}
-
Please use the code tags. And you should always make sure that the posted code still reproduces the problem (but contains no other, unrelated errors), that saves us from guessing. ;)
struct Move Get_MovePointer();
You return an object of type Move and no pointer! That's why you can't check for NULL.
-
What would be the correct syntax to pass a pointer then?
-
Move* TCaracter::Get_MovePointer()
{
return Courant_move;
}
-
So now I have...
Caracter.H
Code:
class TCaracter
{
private:
sf::Sprite Sprite;
struct Move *Moule_move;
struct Move *Stand;
struct Move *Courant_move;
struct Move *Debut_move;
public:
Move* Get_MovePointer();
} ;
Caracter.Cpp
Move* TCaracter::Get_MovePointer()
{
return Courant_move;
}
Main.cpp
Code:
TCaracter Player1;
if (Player1.Get_MovePointer() == NULL)
{
}
Still the same error when I call it in main
-
This is not your real code, is it? The error makes me think that you use a pointer, and access its members with . instead of ->
-
Thanks, your last clue fixed my problem. I have a question:
I am trying to use threads for my caracters, for their animations and stuff. Threads are not inside object classes, they are normal functions besides the main program. The problem I have right now is that if I declare let's say 10 threads with different TCaracter objects with different structure variables (inside the class) but using the same exact class and encapsulated variables, the threads lag alot. They are not in sync and it often crashes.
What does this? Do I need to declare threads in classes or do threads just not work like I thought they did?
-
You shouldn't use threads unless you really need them. And you shouldn't need more than a few threads (2, 3, ...). 10 threads sounds too much for me. What are these threads doing exactly, and why?
Do you really need concurrency on the operations you're performing? Each thread depends on the others to it's progress? Or you can identify single operations, depending on each other, but that can be executed sequentially in a way that each operation can use the results of the previous one?
A game usually does all the updates sequentially, followed by the drawing methods, all on the main program's thread.
About the crashings, may be occurring if you're sharing variables but not protecting their access with locks. Only one thread should access them at a time (this also slows down threads substantially, because they have to wait for each other to access the shared variables).
If you say that they are not in sync, you might be running them concurrently without communication between each other - like, they don't wait for each other, so every thread runs independently with no warranties of sync at all. If threads depends on each others but don't wait for each other, then results are always unpredictable. If they doesn't depend on each other, then consider if you need them running concurrently.
-
Well I figured I needed threads to run each caracter's animation as I am using Sleep() and sleep makes the main program fail.
-
Instead of making them sleep, use a counter that is incremented every game clock.
When the counter reaches a certain value (the time the animation takes to change to the next frame), change the frame and reset the counter.
-
ElapsedTime? I can do that.
But are you saying I should use thos threads or not?
-
You shouldn't