-2

I have defined a Player class to do some operations with, so it is convenient for me to overload some basic operators. Specifically, I want to use < for comparisons between Player objects. As such, I have the following in the class:

bool operator<(const Player& rhs) const {return (*this < rhs );} 

Unfortunately, this has led to problems. Later, when I try to output a vector containing particular elements in my main function, the compiler lets me know that there is no match for the << operand, and it expects std::ostream << Player. Below is the line causing the issue:

vector<Player> playerVec(6);

for (int i = 0; i < 6; i++) {

cout << playerVec[i];

}

Note that I do not actually want to output any Player objects directly to stream, so I don't think I need to overload <<.

I have some idea of what is going on, in that the compiler takes my specific definition for < and then doesn't bother looking for the more general case. My question is, do I need to now overload the << operator to return its general functionality, or is there a simpler solution?

Thanks for any help provided!

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • 1
    Show us the code please. – David G May 31 '13 at 22:01
  • 6
    Isn't this an infinite recursion? – juanchopanza May 31 '13 at 22:02
  • 2
    If you want to output your object, you should overload << operator for that, its not depend on operator< overload – Evgeny Eltishev May 31 '13 at 22:03
  • 1
    A free function would be a better option for comparators. See [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). – chris May 31 '13 at 22:03
  • 7
    The existence of `operator<` is *totally* irrelevant to the non-existence of `operator<<` (which is what your problem actually is, according to your compiler). – Mankarse May 31 '13 at 22:06
  • 1
    @chris: *can* be a better choice, but in this case might well gain little or nothing. A free function would help if and only if a `Player` could be implicitly converted from some other type. For something like a `string`, implicit conversion from string literal is expected. Chances are pretty fair that `Player` doesn't have any single-parameter constructors anyway. – Jerry Coffin May 31 '13 at 22:12
  • 4
    If you're not trying to output `Player` objects to the stream, then what is the intent of this statement: `cout << playerVec[i];` ? Because that's exactly what trying to output `Player` objects to the stream would look like. – Benjamin Lindley May 31 '13 at 22:13
  • @JerryCoffin, You make a good point. – chris May 31 '13 at 22:24
  • What is you expected output for `std::vector`? – Thomas Matthews May 31 '13 at 22:39

2 Answers2

7

I am guessing that you are dealing with two separate issues:

1) You are missing std::ostream& operator<<(std::ostream&, const Player&), which is what you need in order to stream Player objects to std::cout and other output streams

2) You have an infinite recursion in your Player less-than comparison operator <, since the operator you provided calls itself.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1) I am not trying to output Player objects to stream, however, so I did not overload <<. I just want to output a vector. I have edited the original post to show the particular call. 2) Please elaborate. I think I get your point, but how in that case would I compare a player object to the current one? I figured the rhs parameter would suffice, but now I see the issue. – Rome_Leader May 31 '13 at 22:10
  • @Rome_Leader 1) What do you mean by output a vector? Your code literally says stream a Player object. 2) You compare the members of the Player object however you see fit. example: `return id == rhs.id;` – im so confused May 31 '13 at 22:23
  • @Rome_Leader: In order to output a container (vector) of `Player`, you must be able to output an individual `Player`. What are you expecting when outputting a vector of `Player`? – Thomas Matthews May 31 '13 at 22:37
  • Sorry, I guess I didn't realize the implication of making my vector of Player type. As of right now, I'm only trying to output the compared values, so I suppose an integer vector would do. – Rome_Leader May 31 '13 at 22:41
1

I don't think, your operator<() is interfering. Depending on the complexity of your code you can verify this by simply commenting it out and check if you get the same error.

You need to specify a std::ostream & operator<<(std::ostream & os, const Player & p) non-member-function or otherwise the compiler doesn't know what to do writing something like os << myPlayer.

foraidt
  • 5,519
  • 5
  • 52
  • 80