Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pointer problem  (Read 6952 times)

0 Members and 4 Guests are viewing this topic.

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Pointer problem
« Reply #1 on: February 19, 2010, 02:22:37 pm »
Your function declaration in the class TCaracter has the following signature:
Code: [Select]
struct Move Get_MovePointer(string);

But the definition doesn't match, since it belongs to a different class TPersonnage and uses no parameters.
Code: [Select]
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #2 on: February 19, 2010, 02:30:08 pm »
I removed string, but it still doesn't work  :?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Pointer problem
« Reply #3 on: February 19, 2010, 02:30:57 pm »
Quote from: "Xenavar"
I removed string, but it still doesn't work  :?
As stated, the class is still wrong. Once you use TCaracter and once TPersonnage. ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Pointer problem
« Reply #4 on: February 19, 2010, 02:59:37 pm »
And you're missing a ; at end of your TCharacter header
Pluma - Plug-in Management Framework

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #5 on: February 19, 2010, 04:04:16 pm »
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
Code: [Select]
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

Code: [Select]
struct Move TCaracter::Get_MovePointer()
{
return *Courant_move;
}

Main.cpp

Code: [Select]
TCaracter Player1;
if (Player1.Get_MovePointer() == NULL)
{
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Pointer problem
« Reply #6 on: February 19, 2010, 04:15:42 pm »
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. ;)

Code: [Select]
struct Move Get_MovePointer(); You return an object of type Move and no pointer! That's why you can't check for NULL.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #7 on: February 19, 2010, 05:02:27 pm »
What would be the correct syntax to pass a pointer then?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pointer problem
« Reply #8 on: February 19, 2010, 05:21:45 pm »
Code: [Select]
Move* TCaracter::Get_MovePointer()
{
    return Courant_move;
}
Laurent Gomila - SFML developer

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #9 on: February 19, 2010, 07:56:54 pm »
So now I have...

Code: [Select]
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Pointer problem
« Reply #10 on: February 19, 2010, 08:09:57 pm »
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 ->
Laurent Gomila - SFML developer

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #11 on: February 19, 2010, 09:15:39 pm »
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?

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Pointer problem
« Reply #12 on: February 20, 2010, 12:12:10 am »
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.
Pluma - Plug-in Management Framework

Xenavar

  • Newbie
  • *
  • Posts: 40
    • View Profile
Pointer problem
« Reply #13 on: February 20, 2010, 01:47:49 am »
Well I figured I needed threads to run each caracter's animation as I am using Sleep() and sleep makes the main program fail.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Pointer problem
« Reply #14 on: February 20, 2010, 02:08:33 am »
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.
Pluma - Plug-in Management Framework