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

Author Topic: sf:Packet << operator overload problem  (Read 6765 times)

0 Members and 1 Guest are viewing this topic.

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« on: July 01, 2010, 01:23:59 pm »
Hey, I was reading a tutorial on SFML site what says you can customize that packet thing to handle your own classes. I did add the operators there, I have defined those operators inside my player class and it still gives error?

Code is here
main.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include "player.h"

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Samulin RPG");
std::cout << "Opening the main window for game! \n";
player p(App);


    // Start game loop
    while (&App)
    {

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

p.move();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{
private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite getModel();
void move();
float getX();
float getY();
float getHealth();
float getMana();
bool getStatus();
int getLevel();
float getExpirience();
};


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
{
Y += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Up))
{
Y -= 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Right))
{
X += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Left))
{
X -= 0.1f;
this->update();
}
}

float player::getExpirience()
{
return expirience;
}

float player::getX()
{
return X;
}

float player::getY()
{
return Y;
}

float player::getHealth()
{
return health;
}

float player::getMana()
{
return mana;
}

bool player::getStatus()
{
return isAlive;
}

int player::getLevel()
{
return level;
}


// Pakettien omat ylikuormitetut operaattorit

sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.getHealth();
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.getHealth();
//return Packet >> p.getExpirience() >> p.getExpirience() >> p.getLevel() >> p.getMana() /*p.getModel()*/ >> p.getStatus() >> p.getX() >> p.getY();
}



playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <vector>
#include "player.h"

class playerHandler
{
private:
std::vector<player> allPlayers;

public:
playerHandler();
void updateMyPlayer(player Player1);
};


playerHandler.cpp
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler()
{

}



void playerHandler::updateMyPlayer(player Player1)
{
sf::IPAddress serverAddress = sf::IPAddress::LocalHost;

sf::SocketTCP socket;

bool connected = true; // Testing only, will add checkings later..

while(connected)
{
sf::Packet packet;
packet << Player1;
connected = (socket.Send(packet) == sf::Socket::Done);
}

socket.Close();
}



The error I get is
Code: [Select]

1>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
1>playerHandler.cpp
1>.\playerHandler.cpp(21) : error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(226): could be 'std::ostream &sf::operator <<(std::ostream &,const sf::IPAddress &)' [found using argument-dependent lookup]
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(128): or       'sf::Packet &sf::Packet::operator <<(bool)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(129): or       'sf::Packet &sf::Packet::operator <<(sf::Int8)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(130): or       'sf::Packet &sf::Packet::operator <<(sf::Uint8)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(131): or       'sf::Packet &sf::Packet::operator <<(sf::Int16)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(132): or       'sf::Packet &sf::Packet::operator <<(sf::Uint16)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(133): or       'sf::Packet &sf::Packet::operator <<(sf::Int32)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(134): or       'sf::Packet &sf::Packet::operator <<(sf::Uint32)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(135): or       'sf::Packet &sf::Packet::operator <<(float)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(136): or       'sf::Packet &sf::Packet::operator <<(double)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(137): or       'sf::Packet &sf::Packet::operator <<(const char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(138): or       'sf::Packet &sf::Packet::operator <<(const std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(139): or       'sf::Packet &sf::Packet::operator <<(const wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(140): or       'sf::Packet &sf::Packet::operator <<(const std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, player)'
1>player.cpp
1>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\player.cpp(95): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>main.cpp
1>.\main.cpp(46) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\main.cpp(44): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
1>RPG - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========


So where should I actually put those operator overloads? As I put them inside my class and it doesn't seem to work..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf:Packet << operator overload problem
« Reply #1 on: July 01, 2010, 04:19:52 pm »
You forgot to declare them in player.h
Code: [Select]
sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);
Laurent Gomila - SFML developer

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« Reply #2 on: July 01, 2010, 04:27:43 pm »
I declared them, but now I get this:
Code: [Select]

1>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
2>------ Build started: Project: Server, Configuration: Release Win32 ------
2>Compiling...
2>playerHandler.cpp
1>playerHandler.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>.\playerHandler.cpp(16) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\playerHandler.cpp(14): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>player.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\player.cpp(95): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>main.cpp
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(32) : error C2804: binary 'operator <<' has too many parameters
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(33) : error C2804: binary 'operator >>' has too many parameters
2>main.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
1>RPG - 8 error(s), 0 warning(s)
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(32) : error C2804: binary 'operator <<' has too many parameters
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(33) : error C2804: binary 'operator >>' has too many parameters
2>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Server\Release\BuildLog.htm"
2>Server - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========


I have struggled with this some time now, doesn't make sense for me :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf:Packet << operator overload problem
« Reply #3 on: July 01, 2010, 04:46:59 pm »
These operators are not members, declare them outside the class ;)
Laurent Gomila - SFML developer

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« Reply #4 on: July 01, 2010, 05:00:40 pm »
I love this forum! Thanks so much now I got only one error which is related to sf::Packet & operator >> (sf::Packet & Packet, player& p):

It says
Code: [Select]

1>------ Build started: Project: Server, Configuration: Release Win32 ------
2>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
2>Compiling...
2>playerHandler.cpp
1>playerHandler.cpp
1>main.cpp
2>player.cpp
2>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
2>        c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33): or       'sf::Packet &operator >>(sf::Packet &,player &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
2>        while trying to match the argument list '(sf::Packet, float)'
2>main.cpp
1>Linking...
2>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
2>RPG - 1 error(s), 0 warning(s)
1>Generating code
1>Finished generating code
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Server\Release\BuildLog.htm"
1>Server - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The other overloaded function works fine, but the last one seems to make problems :S

Here is the code
player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{
private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite getModel();
void move();
float getX();
float getY();
float getHealth();
float getMana();
bool getStatus();
int getLevel();
float getExpirience();
};

sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
{
Y += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Up))
{
Y -= 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Right))
{
X += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Left))
{
X -= 0.1f;
this->update();
}
}

float player::getExpirience()
{
return expirience;
}

float player::getX()
{
return X;
}

float player::getY()
{
return Y;
}

float player::getHealth()
{
return health;
}

float player::getMana()
{
return mana;
}

bool player::getStatus()
{
return isAlive;
}

int player::getLevel()
{
return level;
}


// Pakettien omat ylikuormitetut operaattorit

sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.getHealth();
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.getHealth();
}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf:Packet << operator overload problem
« Reply #5 on: July 01, 2010, 06:56:29 pm »
Quote
Code: [Select]
return Packet >> p.getHealth();

The variable on the right side of the >> operator is supposed to be modified. It's the same as "p.getHealth() = something", which is obviously wrong because p.getHealth() doesn't return a reference.

Instead of having to define tons of accessors, maybe you shoud simply declare these operators as friends of your class, and directly access the private members.
Laurent Gomila - SFML developer

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« Reply #6 on: July 01, 2010, 07:44:29 pm »
Ok I solved it, thanks, but I got another problem, in the game loop my client has to get all the other players info from the server and draw them so they are in sync, but when it's doing it I can't move my char or anything.. Should I like put the character retriever thing into different thread or something.. I am so new to these things

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf:Packet << operator overload problem
« Reply #7 on: July 01, 2010, 08:14:40 pm »
Typical solutions are:
- one thread + a selector
- no thread + non-blocking sockets

Non-blocking sockets can be a good solution if you already have an update function which is called often enough.
Laurent Gomila - SFML developer

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« Reply #8 on: July 01, 2010, 08:23:43 pm »
I actually tried with a one thread and it works pretty well, but I guess I still try non-blocking sockets, but does it make lag if it constantly checks where the other players are, it's the only solution I can make up.. Hope it doesn't lag.

This is what I got now:

player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{

private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite & getModel();
void move();
float & getX();
float & getY();
float & getHealth();
float & getMana();
bool & getStatus();
int & getLevel();
float & getExpirience();
std::string & getName();

friend sf::Packet & operator << (sf::Packet & Packet, player& p);
friend sf::Packet & operator >> (sf::Packet & Packet, player& p);
};

sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite & player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
Y += 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Up))
Y -= 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Right))
X += 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Left))
X -= 0.1f;

this->update();
}

float & player::getExpirience()
{
return expirience;
}

float & player::getX()
{
return X;
}

float & player::getY()
{
return Y;
}

float & player::getHealth()
{
return health;
}

float & player::getMana()
{
return mana;
}

bool & player::getStatus()
{
return isAlive;
}

int & player::getLevel()
{
return level;
}

std::string & player::getName()
{
return charName;
}


// Pakettien omat ylikuormitetut operaattorit


sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.X << p.Y;
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.X >> p.Y;
}



client main.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include "playerHandler.h"


int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Samulin RPG");
std::cout << "Opening the main window for game! \n";
player p(App);
playerHandler handler(p);
handler.Launch();



    // Start game loop
    while (&App)
    {

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

p.move();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}



client side playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <vector>
#include "player.h"

class playerHandler : public sf::Thread
{
private:
std::vector<player> allPlayers;
player & myPlayer;

virtual void Run();


public:
playerHandler(player & p);
void updateMyPlayer();
};


client side playerHandler.cpp
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler(player & p)
:myPlayer(p)
{

}

void playerHandler::updateMyPlayer()
{
sf::IPAddress serverAddress("127.0.0.1");

sf::SocketTCP socket;

socket.Connect(1000, serverAddress);

bool connected = true; // Testing only, will add checkings later..

while(connected)
{
sf::Packet packet;
packet << myPlayer;
connected = (socket.Send(packet) == sf::Socket::Done);
}

socket.Close();
}

void playerHandler::Run()
{
this->updateMyPlayer();
}



serverside playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <vector>
#include "../RPG/player.h"

class playerHandler
{
private:
sf::SelectorTCP selector;
std::vector<player> players;

public:
playerHandler();
void checkForConnections();

};


playerhandler.cpp serverside
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler()
{
}

void playerHandler::checkForConnections()
{
std::cout << "Checking for connections! \n";
sf::SelectorTCP selector;

sf::SocketTCP listener;

if(!listener.Listen(1000))
{
std::cout << "Can't listen the port! \n";
}

selector.Add(listener);

while(true)
{
unsigned int NbSockets = selector.Wait();

for(unsigned int i = 0; i < NbSockets; i++)
{
sf::SocketTCP socket = selector.GetSocketReady(i);

if (socket == listener)
{
sf::IPAddress address;
sf::SocketTCP client;
listener.Accept(client, &address);
std::cout << "Client connected ! (" << address << ")" << std::endl;

selector.Add(client);
}
else
{
sf::Packet packet;
if(socket.Receive(packet) == sf::Socket::Done)
{
float X;
float Y;
packet >> X >> Y;
std::cout << "X = " << X << " Y = " << Y << "\n";
}
else
{
selector.Remove(socket);
}
}
}
}
}






server main loop
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include "playerHandler.h"


int main()
{
playerHandler handler;
int i = 0;
std::cout << "Server started! \n";
handler.checkForConnections();

std::cin.get();
}


Currently it only receives and sends socket but doesn't sort the players in vector, I guess I'll try to do it today. This is really nice learning btw. Thanks!

samppaa

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf:Packet << operator overload problem
« Reply #9 on: July 02, 2010, 02:29:12 am »
I am in position now where I have to return the player vector to client so it can draw other people so would I need to do a new thread again dedicated to receive information from server? Any ways? Sorry for double post as I am posting from my iphone editin doesn't work. Thanks in advance.