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

Author Topic: Overloading insertion and extraction operator troubles.  (Read 3161 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Overloading insertion and extraction operator troubles.
« on: November 18, 2012, 04:34:38 pm »
I made a class to store commands to be sent from the player to the server. I overloaded the insertion and extraction operators, but it is giving me linking errors. It has been a while since I overloaded an operator so I might be doing it wrong, which I am imaging is the case. Any help would be greatly appreciated.

Here is the file that overloads the operators. It is Visual Studio 2010 Express using SFML 1.6.
Code: [Select]
#pragma once

#include <SFML\Network.hpp>

class CommandFlags
{

public:

bool moveForward;
bool moveBackward;
bool moveLeft;
bool moveRight;

bool shoot;
bool unshoot;

sf::Uint16 mouseX;
sf::Uint16 mouseY;

CommandFlags();
CommandFlags(const CommandFlags &source);
};

sf::Packet &operator >>(sf::Packet &packet, CommandFlags getFlags)
{
return packet >> getFlags.mouseX >> getFlags.mouseY >> getFlags.moveBackward >> getFlags.moveForward >> getFlags.moveLeft >> getFlags.moveRight >> getFlags.shoot >> getFlags.unshoot;
}

sf::Packet &operator <<(sf::Packet &packet, CommandFlags sendFlags)
{
return packet << sendFlags.mouseX << sendFlags.mouseY << sendFlags.moveBackward << sendFlags.moveForward << sendFlags.moveLeft << sendFlags.moveRight << sendFlags.shoot << sendFlags.unshoot;
}

Here is the error I get:

Code: [Select]
1>server-player.obj : error LNK2005: "class sf::Packet & __cdecl operator<<(class sf::Packet &,class CommandFlags)" (??6@YAAAVPacket@sf@@AAV01@VCommandFlags@@@Z) already defined in commandflags.obj

Here is the two files for that class:
Code: [Select]
#include "server-player.h"

Player::Player()
{

}

Player::Player(sf::IPAddress setIP, int setPort, std::string setName)
{
port = setPort;
ip = setIP;
name = setName;
}

sf::IPAddress Player::GetIP()
{
return ip;
}

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

void Player::SetName(std::string setName)
{
name = setName;
}

void Player::Fill(sf::IPAddress playerIP, std::string playerName, unsigned short int playerPort)
{
ip = playerIP;
name = playerName;
port = playerPort;
}

bool Player::IsOccupied()
{
return slotOccupied;
}

void Player::SetCommandFlags(CommandFlags flags)
{
playership->SetCommands(flags);
}

Code: [Select]
#pragma once

#include <string>
#include <queue>

#include <SFML/Network.hpp>

#include "commandflags.h"
#include "server-ship.h"

class Player
{

private:

sf::IPAddress ip;

unsigned short int port;

std::string name;

bool slotOccupied;

ServerShip *playership;

public:

Player();
Player(sf::IPAddress setIP, int setPort, std::string setName);

sf::IPAddress GetIP();

std::string GetName();

void SetName(std::string setName);

void Fill(sf::IPAddress playerIP, std::string playerName, unsigned short int playerPort);

bool IsOccupied();

void SetCommandFlags(CommandFlags flags);

void SetPlayerShip(ServerShip *setShip);
ServerShip* GetPlayerShip();
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Overloading insertion and extraction operator troubles.
« Reply #1 on: November 18, 2012, 05:49:48 pm »
A function cannot be defined in a header, unless it is inline, otherwise it ends up being compiled in every .cpp that includes the header, and your linker complains about multiple definitions of the same function.
Laurent Gomila - SFML developer

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Overloading insertion and extraction operator troubles.
« Reply #2 on: November 18, 2012, 10:05:28 pm »
I moved the operator overload functions into the .cpp file for the commandflags class and now I get this:

1>server.cpp(119): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Overloading insertion and extraction operator troubles.
« Reply #3 on: November 18, 2012, 11:39:24 pm »
You must still declare it in the header.
Laurent Gomila - SFML developer

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Overloading insertion and extraction operator troubles.
« Reply #4 on: November 19, 2012, 02:33:06 am »
Doh! Duh!