Help with operator overloading

Error: warning C4717: 'operator>>' : recursive on all control paths, function will cause runtime stack overflow

1
2
3
4
5
6
7
8
9
sf::Packet& operator <<(sf::Packet& _packet, const SERVER_REQUEST& _request)
{
	return _packet << static_cast<sf::Int8>(_request);
}

sf::Packet& operator >>(sf::Packet& _packet, SERVER_REQUEST& _request)
{
	return _packet >> static_cast<SERVER_REQUEST>(_request);
}


Server_Request:

1
2
3
4
enum SERVER_REQUEST
{
	REQUEST_JOIN
};


sf::Packet takes, amount other things, most numerical types. I'm trying to convert my enum to an int, send it, then get the enum from the packet on server side.

(if needed): http://www.sfml-dev.org/documentation/2.0/classsf_1_1Packet.php
Last edited on
bump. i had this working before but i forgot what i did. i feel like it's something simple that i'm missing :/
Don't you think these should be a friend functions.
They're not in a class. I just put them in my network header header with all my other network structs.

so now it runs, but when I use the '>>' operator I get a stack overflow.

"Stack overflow (parameters: 0x00000001, 0x00FA2FD4)."

.h:

1
2
sf::Packet& operator <<(sf::Packet& _packet, const NETWORK_REQUEST& _request);
sf::Packet& operator >>(sf::Packet& _packet, NETWORK_REQUEST& _request);


.cpp:

1
2
3
4
5
6
7
8
9
10
11

sf::Packet& operator <<(sf::Packet& _packet, const NETWORK_REQUEST& _request)
{
	return _packet << static_cast<sf::Int8>(_request);
}

sf::Packet& operator >>(sf::Packet& _packet, NETWORK_REQUEST& _request)
{
	return _packet >> static_cast<NETWORK_REQUEST>(_request);
}


Pls help.
Last edited on
Why do you have two versions? What does the second do?
What do you mean? There's one input and one output operator overload. I need to input data to packets and also get data from packets.

How it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//send data
sf::Packet packet;
NETWORK_REQUEST request = NETWORK_REQUEST::REQUEST_JOINGAME;

packet << request;
socket.send(packet);

//get data
sf::Packet packet;
NETWORK_REQUEST request;

socket.receive(packet);
packet >> request;

if(request == NETWORK_REQUEST::REQUEST_JOINGAME)
blah blah blah


sf::Packet doc if needed to understand: http://www.sfml-dev.org/documentation/2.0/classsf_1_1Packet.php

There's also a compiler warning: 'operator>>' : recursive on all control paths, function will cause runtime stack overflow
Last edited on
My error.

If you had only one of these, would it compile? In other words, is only one of the two the problem?

The input operator calls itself recursively. Why don't you read into, say sf::Int8 variable, and then store the value into _request?
You can store ints in an enum? How would that fix it?

>The input operator calls itself recursively.

can you explain why that happens? I'm not that experienced with overloading operators yet.
DELETED - I was looking at the wrong operator
Last edited on
An operator is just a function that has some syntactic sugar. Rather than calling op( lhs, rhs) we can write lhs op rhs.

Your input operator is similar to:
1
2
3
4
int & foo( int & lhs, int & rhs )
{
  return foo( lhs, rhs );
}

That is infinite recursion.

You do have a static_cast, but what does a cast from type T to type T does for a reference? Nothing?
Even if it did something, the very same operator>> (sf::Packet&, T&) is still the closest match.

You can store ints in an enum?

That depends. Each enum has some underlying type.
The C++11 did change the rules for enums.
http://www.cplusplus.com/forum/general/147341/

How would that fix it?
1
2
3
4
5
6
7
sf::Packet& operator >>(sf::Packet& _packet, NETWORK_REQUEST& _request)
{
  sf::Int8 value = 0;
  _packet >> value // calls different operator
  _request = value; // might need explicit conversion
  return _packet;
}
Topic archived. No new replies allowed.