It says: "error: no match for 'operator==' (operand types are 'Player***' and 'const Player')".
I have another version of this program where vector 'opponents' has the type 'vector<string>' instead of 'vector<Player***>', and it works perfectly fine.
However, I really need to create this 'vector<Player***>' type vector, so this is just a functional workaround for now. The reason why I need this type of vector is because in the future I will need something like this:
1 2 3 4 5 6 7 8 9
for (int j = 0; i < round; j++) {
for (int i = 0; i < round ; i++) {
participant[j].oppgameswinpercentage += participant[j].(***opponents).gameswinpercentage / round; //CREATE THIS
}
}
Two or three levels of pointer indirection gets tricky. Wanting to use them sounds like a better design is needed.
This is a 4 dimensional thing: vector<vector<Player**> > pairings;
Why does Player have vector<Player***> opponents; ? That's a 4 dimensional list of opponents !
Try to do everything with better combinations of C++ STL containers instead. I gather you have pairs of things (pairings) , the STL has std::pair<>.
Also, try to avoid new. The problem with new is that if an exception is thrown by anything, delete is never reached. Although I guess you are using it here because you wanted pointers.
I think you need to make better use of functions. A function should only do 1 conceptual thing, so they should be short - less than 40 LOC say, usually less. The main function should call a series of functions.
Thanks for the reply. I will definitely organize everything in functions, I had that planned.
Player has vector<Player***> opponents; because every player will have fields that need to take data from all their opponents. After every round, this vector will add a pointer at the opponent a player has played against. This way, you keep track of all the opponents that a player has faced. With my limited knowledge I don't know any other way to do that.
For the pairings thing, it seems like I'd like to use std::pair. It should be easy, I'll figure out the way to do that.
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
class Player {
public:
Player() { id = ++auto_increment; }
int id;
std::set<int> met_id;
friend std::ostream& operator<<(std::ostream& os, const Player& out);
friendbooloperator==(const Player& one, const Player& two);
private:
staticint auto_increment;
};
int Player::auto_increment {0};
std::ostream& operator<<(std::ostream& os, const Player& out)
{
os << "Player " << out.id << " have already met: ";
for(int i : out.met_id) { os << "Player " << i << "; "; }
return os << '\n';
}
booloperator==(const Player& one, const Player& two)
{ return one.id == two.id; }
void waitForEnter();
int main()
{
std::vector<Player> partecipants(5);
std::cout << "Competition starts...\n";
for(auto& a : partecipants) {
for(auto& b : partecipants) {
if(a == b) { continue; }
std::cout << "Player " << a.id << " is going to meet "
<< " player " << b.id << '\n';
a.met_id.insert(b.id); // no need to check if succeed
b.met_id.insert(a.id); // no need to check if succeed
}
}
std::cout << "Let's verify if all data have been put down.\n";
for(constauto& a : partecipants) { std::cout << a; }
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Output:
Competition starts...
Player 1 is going to meet player 2
Player 1 is going to meet player 3
Player 1 is going to meet player 4
Player 1 is going to meet player 5
Player 2 is going to meet player 1
Player 2 is going to meet player 3
Player 2 is going to meet player 4
Player 2 is going to meet player 5
Player 3 is going to meet player 1
Player 3 is going to meet player 2
Player 3 is going to meet player 4
Player 3 is going to meet player 5
Player 4 is going to meet player 1
Player 4 is going to meet player 2
Player 4 is going to meet player 3
Player 4 is going to meet player 5
Player 5 is going to meet player 1
Player 5 is going to meet player 2
Player 5 is going to meet player 3
Player 5 is going to meet player 4
Let's verify if all data have been put down.
Player 1 have already met: Player 2; Player 3; Player 4; Player 5;
Player 2 have already met: Player 1; Player 3; Player 4; Player 5;
Player 3 have already met: Player 1; Player 2; Player 4; Player 5;
Player 4 have already met: Player 1; Player 2; Player 3; Player 5;
Player 5 have already met: Player 1; Player 2; Player 3; Player 4;
Press ENTER to continue...