I have a small problem. I'm currently writing a code for tournament handling and I came out with an idea that the best way to keep teams in order in memory will be a list.
Now, I'm trying to sort list cointaing Team class that is containg points records.
Here's the class declaration:
#include "player.h"
#include <string>
class Team {
Player** Gracz;
std::string Name;
int TP, STP;
int Total;
public:
Team();
Team(Player* gracz1, Player* gracz2, Player* gracz3, Player* gracz4, Player* gracz5, Player* gracz6, std::string name);
~Team();
void SetTeam();
void SetTeam(Player gracz1, Player gracz2, Player gracz3, Player gracz4, Player gracz5, Player gracz6, std::string name);
void SetTP(int tp);
void SetSTP(int stp);
std::string GetTeam();
int GetTotal();
int GetTP();
int GetSTP();
bool operator<(Team& team);
bool operator>(Team& team);
void PrintTeam();
};
And here's the program code:
#include <iostream>
#include "player.h"
#include "team.h"
#include <list>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
Player *p;
Team *t1, *t2, *t3, *t4;
list<Team> x;
list<Team>::iterator it;
p=new Player("a","a","a");
t1=new Team(p,p,p,p,p,p,"A");
t2=new Team(p,p,p,p,p,p,"B");
t3=new Team(p,p,p,p,p,p,"C");
t4=new Team(p,p,p,p,p,p,"D");
x.push_back(*t1);
x.push_back(*t2);
x.push_back(*t3);
x.push_back(*t4);
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
cout<<" "<<(*it).GetTeam();
cout<<endl;
t1->SetTP(15);
t2->SetTP(4);
t3->SetTP(8);
t4->SetTP(8);
t3->SetSTP(15);
t4->SetSTP(65);
x.sort();
cout<<"Turniej: "<<endl;
for(it=x.begin();it!=x.end();++it)
cout<<" "<<(*it).GetTeam();
cout<<endl;
return 0;
}
So I'd like to sort list by firstly TPs and then by STPs and it's included in declaration of overloaded operator <. When I'm printing list, I get A,B,C,D before the sorting and the same after the sorting, instead of A,D,C,B after. Where's my mistake?
Thanks for help.
Here the object is copied, and its copy is pushed into a container:
x.push_back(*t1);
/* the same for others */
Here you modify the original object, but the copy in the container is unchanged:
t1->SetTP(15);
Related
I am creating a C++ project and it's a console application and I have to show premiere league table with using structs , arrays ,file stream and input and output I made a struct but I don't have and idea what to do next and it has to be on console not GUI. I tried making a struct but don't know what to do
#include <iostream>
using namespace std;
#include <string>
struct standing
{
int position;
char name[20];
int matches;
int win;
int draw;
int lose;
int GD;
int points;
};
#include <string>
#include <vector>
struct Team
{
private:
int rank;
std::string name;
int matches;
int win;
int draw;
int lose;
int GD;
int points;
};
struct League
{
private:
std::vector<team> teams;
}
Say we have these two structs. One struct for a team the other one for the whole league.
We can now add some constructors for Team struct (trivial) and League struct and public methods.
struct League
{
public:
void addTeam(Team team_)
{
teams.push_back(team_);
};
Team getChampion()
{
// simple algo to get winning team
}
std::pair<Team, Team> getRegulated(){
// simple algo to get regulated teams
};
private:
std::vector<team> teams;
};
https://www.cplusplus.com/doc/tutorial/structures/
I have the following code:
Card.h:
#include <string>
using namespace std;
class Card
{
public:
Card(string name);
~Card() {};
string GetName();
private:
string Name;
};
Card.cpp:
#include "Card.h"
using namespace std;
Card::Card(string name) {Name=name;};
string Card::GetName() {return Name;}
Deck.h:
#include "Card.h"
#include <vector>
class Deck {
public:
Card& DrawCard();
void AddCardToDeck(Card& c);
Deck();
~Deck();
private:
std::vector <Card> cardsindeck;
};
Deck.cpp:
#include "Deck.h"
#include <vector>
#include <iostream>
using namespace std;
Card& Deck::DrawCard() {
//cout << cardsindeck.back().GetName()<<" was drawn "<<endl;
Card &c = cardsindeck.back();
cout << c.GetName()<<" was drawn "<<endl;
cardsindeck.pop_back();
cout << c.GetName()<<" popped from deck "<<endl;
return c;
}
Deck::Deck()
{
}
Deck::~Deck()
{
}
void Deck::AddCardToDeck(Card& c) {
cardsindeck.push_back(c);
}
Player.h:
#include "Deck.h"
#include <vector>
using namespace std;
class Player {
public:
void Beginning();
Player(Deck _deck);
~Player() {};
private:
vector <Card> cardsindeck;
Deck deck;
};
Player.cpp:
#include "Player.h"
using namespace std;
Player::Player(Deck _deck)
{
this->deck = _deck;
}
void Player::Beginning()
{
Card& c = deck.DrawCard();
}
main.cpp:
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Deck aDeck;
vector <Card> aHand;
Card c=Card("THIS THIS GREAT PLAYER");
Card& c1 =c;
aDeck.AddCardToDeck(c1);
Player P = Player(aDeck);
P.Beginning();
return 0;
}
The output I get is:
THIS THIS GREAT PLAYER was drawn
�\IS GREAT PLAYER popped from deck
Why does the 2nd line have that weird chars in place of "THIS THIS" ?
You have undefined behavior in this function:
Card& Deck::DrawCard() {
// ...
Card &c = cardsindeck.back();
// ...
cardsindeck.pop_back();
cout << c.GetName()<<" popped from deck "<<endl;
return c;
}
First, you alias the last element of cardsindeck with a reference called c. This is fine, and access to its member function is fine, too. Then, you remove the element from the container with cardsindeck.pop_back();. From the docs on std::vector::pop_back, we see that
No iterators or references except for back() and end() are invalidated.
And that's the issue here. You are having a reference to back(), and that is invalidated. It must be - you are deleting the element in the vector that c refers to from the container. Accessing its members then e.g. by GetName() is UB, then.
You can easily fix the issue by copying the return value of cardsindeck.back() like this:
Card c = cardsindeck.back();
// ^^ No reference. The last element is copied
cardsindeck.pop_back(); // Doens't affect the copied instance above
return c;
Note that this requires changing the signature of the member function to
Card Deck::DrawCard()
where the return value is no reference anymore.
I have a class Team and a class Ball and I create a vector in constructor of Team that is filled with objects of another class called Player. So I want to use this vector in the class Ball but even though I define it as extern (public) compiler keeps telling me that I have undefined reference to team that is my vector. Here follows the code of Team.cpp and Ball.cpp
Team.h
#define TEAM_H
#include <iostream>
#include <vector>
#include "Player.h"
using namespace std;
extern vector<Player> team;
class Team {
public:
Team();
void fillTeamVector(vector<Player>&);
private:
string teamName;
int faults;
int passes;
int goals;
};
#endif // TEAM_H
Team.cpp
#include "Team.h"
#include <vector>
#include <iostream>
#include "Player.h"
#include "Attacker.h"
#include "Defender.h"
#include "Ball.h"
Team::Team()
{
extern vector<Player> team;
fillTeamVector(team);
}
void Team::fillTeamVector(vector<Player>& team){
// do stuff and store them on vector team
}
And here follows the code for Ball.h note that I commented all the methods that don't affect the problem.
#ifndef BALL_H
#define BALL_H
#include "Player.h"
class Ball
{
public:
Ball();
Player* current;
Player* previous;
/*void setX_ball(int);
int getX_ball() const;
void setY_ball(int);
int getY_ball() const;*/
void assign();
//void changeCurrentToPrevious();
//void changeNextToCurrent(Player*);
private:
int X_ball;
int Y_ball;
};
#endif // BALL_H
Here is the code for Ball.cpp note that in method assign if I create a new (and obviously different vector of Player named team it will compile correctly)
#include "Ball.h"
#include "Team.h"
#include "Player.h"
extern vector<Player> team;
Ball::Ball()
: X_ball(2),
Y_ball(5)
{
current = NULL;
previous = NULL;
}
void Ball::assign(){
//vector<Player> team;
int x;
int y;
x=(team[0].getX())-X_ball;
y=(team[0].getY())-Y_ball;
int min=x+y;
int k=0;
for (int i=1; i<team.size(); i++){
x=(team[i].getX())-X_ball;
y=(team[i].getY())-Y_ball;
int sum=x+y;
if(sum<min){
k=i;
}
}
current = &team[k];
}
By doing
extern vector<Player> team;
you only declare the variable.
In one source file you must actually define the variable:
vector<Player> team;
Note the lack of extern in the definition.
Also note that this has to be done in the global scope, since you want a global variable. So it has to be defined outside of any functions or classes or namespaces.
I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.
#include "player.h"
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
player a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
vector <player> a;
player a;
}
team::addPlayer(player)
{
a.push_back(a);
}
If more info is needed please ask. Thank you in advance for any help.
I assume this is what you meant:
#include "player.h"
#include <vector>
class team
{
public:
team();
void addPlayer(player);
void deletePlayer(int);
double getTotalCost();
int getPlayerCount();
double inBank();
string toString();
private:
vector<player> a;
int playerId;
double bank;
int i;
};
#include "../../std_lib_facilities.h"
#include "team.h"
team::team()
{
}
team::addPlayer(player p)
{
a.push_back(p);
}
You shoud have your vector variable a member of your class or create it on heap a keep the pointer to it. Now you are creating your vector on a stack in teem constructor. It will deleted when constructor is finished.
Also you can't use name a for both player and vector. I recommend you to read some C++ books first.
There's so much wrong with this I don't know where to begin, you declare a SINGLE player, here:
private:
player a; // why not just call every variable in your program "a"?
And then in your constructor of the team:
team::team()
{
vector<player> a; // a vector that will be destroyed on exit from constructor
player a; // another single player, but you've just defined 'a' so you should get a compiler error along the lines of redefinition.
}
I suspect you want something like:
#include <vector>
#include <string>
#include "player.h"
class team
{
private:
std::vector<player> m_Players; // players
public:
void addPlayer(const player& _player) { m_Players.push_back(_player); }
}; // eo class team
What do u need ? Store players in your class team ?
#include <iostream>
#include <vector>
using namespace std;
class Player
{
public:
Player(int id) { m_id = id; }
int GetId() {return m_id; }
private:
int m_id;
};
class team
{
public:
team(){};
void AddPlayer(Player p) {m_arr.push_back(p);}
size_t Size(){ return m_arr.size();}
Player GetPlayer(size_t index) {return m_arr[index];}
private:
vector<Player> m_arr;
};
void main()
{
team t;
for (int i =0; i < 10; ++i)
{
t.AddPlayer(Player(i));
}
for (int i =0; i < t.Size();++i)
{
cout << "Player [" << i + 1 << "] with id: " << t.GetPlayer(i).GetId() << endl;
}
}