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

Author Topic: Compile error when writing own class. - HELP!  (Read 5283 times)

0 Members and 1 Guest are viewing this topic.

YellowShadow

  • Newbie
  • *
  • Posts: 29
    • AOL Instant Messenger - ehwatsupdoc21
    • View Profile
Compile error when writing own class. - HELP!
« on: January 09, 2009, 04:09:48 am »
I'm kinda new to C++ but I love how easy it was for me to get a simple sprite setup and moving with SFML. Now I'm trying to refactor my code so that I take everything related to "player" outside of the int main call and put it in its own class.

So I wrote out my header file and started to write the actual .cpp file when I ran into errors compiling. I don't know what I'm doing wrong.

If anyone would be kind enough to help me or point me the direction of help that would be great.

I have included the header, cpp, and compile log below.

BTW: I'm using SFML-1.4, CodeBlocks with MinGW. I'm also compiling on Windows.

Code: [Select]

#ifndef PLAYER_H
#define PLAYER_h

#include <string>

#include <SFML/Graphics.hpp>

class Player
{
    private:
        std::string name;
        sf::RenderWindow window;
        sf::Vector2 position;
        bool isWalking;

    public:
       Player(std::string name, sf::Vector2 position, sf::RenderWindow window);
       ~Actor();

       void update();
       void draw();

       std::string getName();
       sf::Vector2 getPosition();
};

#endif


Code: [Select]

#include "Player.h"

Player::Player(std::string name, sf::Vector2 position, sf::RenderWindow *window)
    : name(name), position(position), window(window)
{

}

Player::~Player()
{

}

void Player::update()
{

}

void Player::draw()
{

}

std::string Player::getName()
{
    return name;
}

sf::Vector2 Player::getPosition()
{
    return position;
}


Code: [Select]

-------------- Build: Debug in Client ---------------

Compiling: src\main.cpp
Compiling: src\Player.cpp
In file included from C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:1:
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:13: error: using-declaration for non-member at class scope
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:13: error: expected `;' before "position"
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:17: error: `sf::Vector2' is not a type
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:17: error: ISO C++ forbids declaration of `position' with no type
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:18: error: expected class-name before '(' token
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:24: error: using-declaration for non-member at class scope
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:24: error: expected `;' before "getPosition"
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:3: error: `sf::Vector2' is not a type
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:4: error: ISO C++ forbids declaration of `position' with no type
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:4: error: prototype for `Player::Player(std::string, int, sf::RenderWindow*)' does not match any in class `Player'
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:9: error: candidates are: Player::Player(const Player&)
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.h:17: error:                 Player::Player(std::string, int, sf::RenderWindow)
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp: In constructor `Player::Player(std::string, int, sf::RenderWindow*)':
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:4: error: class `Player' does not have any field named `position'
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp: At global scope:
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:10: error: definition of implicitly-declared `Player::~Player()'
C:\Documents and Settings\Dro\Desktop\GunKnights\client_code\src\Player.cpp:29: error: expected constructor, destructor, or type conversion before "Player"
Process terminated with status 1 (0 minutes, 1 seconds)
15 errors, 0 warnings


Again, thanks in advance to anyone who could help me.

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
Compile error when writing own class. - HELP!
« Reply #1 on: January 09, 2009, 08:49:38 am »
Code: [Select]
#ifndef PLAYER_H
#define PLAYER_h


Not good :D
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: Compile error when writing own class. - HELP!
« Reply #2 on: January 09, 2009, 09:31:02 am »
Quote from: "YellowShadow"

Code: [Select]

#ifndef PLAYER_H
#define PLAYER_h

#include <string>

#include <SFML/Graphics.hpp>

class Player
{
    private:
        sf::RenderWindow window;
    public:
       Player(std::string name, sf::Vector2 position, sf::RenderWindow window);
};

#endif


Code: [Select]

#include "Player.h"

Player::Player(std::string name, sf::Vector2 position, sf::RenderWindow *window)
    : name(name), position(position), window(window)
{

}




also not good .. declaration wants a copy but implementation wants a pointer.. if I where you I would change window variable to be a pointer.
//Zzzarka

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Compile error when writing own class. - HELP!
« Reply #3 on: January 09, 2009, 02:31:25 pm »
Quote from: "zarka"
if I where you I would change window variable to be a pointer.
Or a reference, if it doesn't change during the lifetime of a Player instance.

Generally, you should pass class types rather as const-reference than as copy. Especially huge classes like Windows, which can't be copied (for a good reason).

Code: [Select]
class Player
{
    private:
        sf::RenderWindow&    MyRenderWindow;     // reference to an extern Window
        sf::Vector2f         MyPosition;
        std::string          Name;

    public:
        ...
};

Player::Player(const std::string& Name, const sf::Vector2f& Position, // here Vector2f instead of Vector2
               sf::RenderWindow& Window) // pass by reference
: MyName(Name)
, MyPosition(Position)
, MyRenderWindow(Window)
{
}


But apart from this, I find the design a little bit questionable. Do you think it's good if the Player knows the Window he is drawn on? I would rather create a more central class (like Game) which contains Player, Enemies and so on. Then you could let the Game class handling Window stuff.

To achieve even more abstraction, you can outsource the rendering functionality into an own class (Renderer, GraphicManager or similar). Like that, neither Player nor Game have to know anything about graphics.

Just an advice, maybe it is not appropriate at the moment, but abstraction is always worth a thought... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

YellowShadow

  • Newbie
  • *
  • Posts: 29
    • AOL Instant Messenger - ehwatsupdoc21
    • View Profile
Re: Compile error when writing own class. - HELP!
« Reply #4 on: January 10, 2009, 03:15:23 am »
Quote from: "Nexus"

But apart from this, I find the design a little bit questionable. Do you think it's good if the Player knows the Window he is drawn on? I would rather create a more central class (like Game) which contains Player, Enemies and so on. Then you could let the Game class handling Window stuff.

To achieve even more abstraction, you can outsource the rendering functionality into an own class (Renderer, GraphicManager or similar). Like that, neither Player nor Game have to know anything about graphics.

Just an advice, maybe it is not appropriate at the moment, but abstraction is always worth a thought... ;)


If I were to do create my own Game class, what should I include in it? And also how would I make the Player class then, because right now I only know how to draw with sf::RenderWindow::Draw().

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Compile error when writing own class. - HELP!
« Reply #5 on: January 15, 2009, 01:48:52 am »
Quote from: "YellowShadow"
If I were to do create my own Game class, what should I include in it? And also how would I make the Player class then, because right now I only know how to draw with sf::RenderWindow::Draw().
The design matter is not extremely important, I haven't emphasized design questions for a long time, either.

At the moment, I have the Game class holding the player, containers of enemies, tiles and other game-elements. Another class (at me GraphicManager) has a reference to a quasi-global sf::RenderWindow (inside a Singleton). Game also contains a reference or a pointer to such a GraphicManager instance (which is itsself stored globally or inside a hierarchically higher class or singleton).
Code: [Select]
class GraphicManager
{
    public:
        GraphicManager(sf::RenderWindow& NewRenderWindow);

        void Draw(const Player& CurrentPlayer);
        void Draw(const Enemy& CurrentEnemy);
        void Draw(const Tile& CurrentTile);

    private:
        sf::RenderWindow& MyRenderWindow;
        sf::View          MyView;
        sf::Image         MyImage;
        ...
};

So the Draw() member-functions are overloaded for the specific game elements like Player, Enemy, and so on. Internally, they know how to create a sprite and draw that. For example:
Code: [Select]
void GraphicManager::Draw(const Player& CurrentPlayer)
{
    sf::Sprite Sprite(MyImage, CurrentPlayer.GetPosition());
    Sprite.SetSubRect(...);
    ...
    MyRenderWindow.Draw(Sprite);
}

The advantage is more abstraction. Everything concerning rendering is handled by the GraphicManager class. You can easily use the methods in Game:
Code: [Select]
void Game::Output() const
{
    MyGraphicManager.Draw(MyPlayer);

    for (std::list<Enemy>::const_iterator i = MyEnemies.begin(); i != MyEnemies.end(); ++i)
        MyGraphicManager.Draw(*i);
}

In contrast, the Player class stores only the data which is really relevant for a player.
Code: [Select]
class Player
{
    public:
        const sf::Vector2f& GetPosition() const;
        ...

    private:
        sf::Vector2f  MyPosition;
        int           MyHitpoints;
        ...
};


But unfortunately, I couldn't force through this system very consequently. Sometimes I still have small sprites I create locally (outside GraphicManager) and just pass the sprite to draw. But at least, I was able to outsource the rendering itsself, images, views, the window-reference, and all the graphic-specific stuff.

That's just the way I am doing it. As always, there are multiple possibilities. I hope I could make this fairly understandable, if you have got any questions, just ask. :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: