7
« 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
#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
#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
#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
#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
#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
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..