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/
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
My code is raising this error:
supportCrew.cpp:(.text+0x15): undefined reference to `sportsPsychologist::sportsPsychologist()'
supportCrew.cpp:(.text+0x25): undefined reference to `Physiotherapist::Physiotherapist()'
supportCrew.cpp:(.text+0x35): undefined reference to `trainer::trainer()'
supportCrew.cpp:(.text+0x47): undefined reference to `trainer::trainer()'
The two relevent classes/cpp files are supportCrew and Person
Person.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <chrono>
#include <random>
#include <algorithm>
Person::Person(std::string pName, int pAge, int pExperience) {
name = pName;
age = pAge;
experience = pExperience; //years of experience
};
Athlete::Athlete(std::string name, int age, int experience, std::string gender, double height, double weight)
{
gender = gender;
height = height;
weight = weight;
};
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
readinessScore = readiness;
recoveryScore = recovery;
};
sportsPsychologist::sportsPsychologist(std::string name, int age, int experience, int pressure, int injury, int successFail) {
pressurescore = pressure;
injuryscore = injury;
successfailscore = successFail;
};
trainer::trainer(std::string name, int age, int experience,std::string specialization, int performance, int consistency) {
specialization = specialization;
performanceScore = performance;
consistencyScore = consistency;
};
Person.h
#ifndef PERSON
#define PERSON
class Person {
std::string name;
int experience; //in years
int age;
public:
Person(std::string pName, int pAge, int pExperience);
Person();
virtual ~Person(){};
std::string getName() {return randomName();};
int getAge(){return randomAge();};
int getExperience(){return randomExperience();};
void printData(std::string, int, int);
std::string randomName();
int randomAge();
int randomExperience();
};
class Athlete: public Person{
private:
std::string gender;
double height;
double weight;
public:
Athlete(std::string pName, int pAge, int pExperience, std::string gender, double height, double weight);
Athlete();
virtual ~Athlete(){};
std::string &getGender(){return gender;};
std:: string randomGender();
double randomHeight();
double randomWeight();
};
class Physiotherapist: public Person{
private:
int recoveryScore;
int readinessScore;
public:
Physiotherapist(std::string pName, int pAge, int pExperience, int recoveryScore, int readinessScore);
Physiotherapist();
int &getscoreRecovery(){return recoveryScore;};
int &getscoreReadiness(){return readinessScore;};
};
class sportsPsychologist: public Person {
private:
int pressurescore;
int injuryscore;
int successfailscore;
public:
sportsPsychologist(std::string pName, int pAge, int pExperience, int pressureScore, int injuryScore, int successfailScore);
sportsPsychologist();
int &getscorePressure(){return pressurescore;};
int &getscoreInjury(){return injuryscore;};
int &getscoreSuccues_Fail(){return successfailscore;};
};
class teamManager: public Person {
public:
teamManager(std::string pName, int pAge, int pExperience);
teamManager();
};
class trainer: public Person {
private:
std::string specialization;
int performanceScore;
int consistencyScore;
public:
trainer(std::string pName, int pAge, int pExperience, std::string specialization, int performanceScore, int cocsistencyScore);
trainer();
std::string &getSpecialization(){return specialization;};
int &getscorePeformance(){return performanceScore;};
int &getscoreConsistency(){return consistencyScore;};
};
#endif
supportCrew.h
#ifndef SUPPORTCREW
#define SUPPORTCREW
#include "Person.h"
class supportCrew {
private:
sportsPsychologist Psychologist;
Physiotherapist physio;
trainer trainer1;
trainer trainer2;
public:
supportCrew(sportsPsychologist,Physiotherapist,trainer,trainer);
supportCrew();
};
#endif
supportCrew.cpp
#include <iostream>
#include <sstream>
#include <fstream>
#include "compDay.h"
#include "Competion.h"
#include "Events.h"
#include "Location.h"
#include "Octathlon.h"
#include "Person.h"
#include "supportCrew.h"
#include "theTeam.h"
#include "weatherSystem.h"
supportCrew::supportCrew() {
}
It seem the skill you are missing is how to call base class constructors. So
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
readinessScore = readiness;
recoveryScore = recovery;
};
should really be
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)
: Person(name, age, experience) {
readinessScore = readiness;
recoveryScore = recovery;
}
If you do that then the Person::Person() constructor isn't required because you are explicitly calling the Person::Person(std::string pName, int pAge, int pExperience); constructor.
Of course you should be using initialization lists everywhere. So the above code is even better written like this
Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)
: Person(name, age, experience)
, readinessScore(readiness)
, recoveryScore(recovery) {
}
If you don't call the base class construcor from an initialization list, then the default base class constructor will be called implicitly. I guess this is why you've decalred all these default constructors. But if you code it like I show you above then you (probably) don't need all these default constructors.
Learning C++ and getting error "no matching function for call to 'Weapon::Weapon(int, const char [4], int, int)'" when creating a weapon istance
Weapon MyWeapon(1,"test",4,1);
Where is the bug?
item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <iostream>
using namespace std;
class Item
{
public:
string name;
int weight;
int value;
Item(string n, int w, int v);
};
#endif
item.cpp
#include <iostream>
#include <string>
#include <vector>
#include "item.h"
using namespace std;
Item::Item(string name, int weight, int value)
{
name=name;
weight=weight;
name=value;
}
weapon.h
#ifndef WEAPON_H
#define WEAPON_H
#include "item.h"
class Weapon : public Item
{
public:
int damage;
Weapon();
Weapon(int damage);
};
#endif
weapon.cpp
#include <iostream>
#include <string>
#include <vector>
#include "weapon.h"
using namespace std;
Weapon::Weapon( damage): Item(name, weight, value){}
You need to explicitly specify all the Item arguments in the Weapon constructor.
Change the header like this:
Weapon(int damage, string n, int w, int v);
And the implementation like that:
Weapon::Weapon(int damage, string n, int w, int v): damage(damage), Item(name, weight, value){}
You're getting "no matching function for call to Weapon::Weapon(int, const char [4], int, int)" because there isn't a constructor with that signature. Change the Weapon definition to something like this:
class Weapon : public Item
{
public:
int damage;
Weapon(int damage, std::string name, int weight, int value);
};
Then fix up weapon.cpp to send those arguments to the Item constructor:
Weapon::Weapon(int damage, std::string name, int weight, int value) :
Item(name, weight, value),
damage(damage)
{ }
Also a quick side note: using namespace std is considered a bad practice.
So I'm a begging in C++, Im making a program to calculate the area and the perimeter of a triangle when the user inputs the base, height and sides. I have 2 classes, Area and Perimeter, I need to access the variable "base" from Area and use them in Perimeter, since I need the base to calculate perimeter. How can I do this? Also would it be more effective to use one class for this program?
Area.h
#ifndef AREA_H
#define AREA_H
#include <iostream>
using namespace std;
class Area
{
private:
int height;
public:
int base;
Area();
int calcArea();
};
#endif // AREA_H
Area.cpp
#include "Area.h"
#include <iostream>
using namespace std;
Area::Area()
{
cin >> base;
cin >> height;
};
int Area::calcArea(){
int answer;
answer = (base * height)/2;
return answer;
}
Perimeter.h
#ifndef PERIMETER_H
#define PERIMETER_H
#include "Area.h"
#include <iostream>
using namespace std;
class Perimeter
{
private:
int s1;
int s2;
public:
Perimeter();
int calcP();
};
#endif // PERIMETER_H
Perimeter.cpp
#include "Perimeter.h"
#include "Area.h"
#include <iostream>
using namespace std;
Perimeter::Perimeter()
{
cin >> s1;
cin >> s2;
}
int Perimeter:: calcP(){
int answer;
answer = s1 + s2 + base ;
return answer;
}
Instead of creating two classes, Create a class Triangle.
class Triangle
{
private:
int base;
int height;
public:
Triangle();
int CalculatePerimeter();
int CalculateArea();
};
Define the functions.
Create objects of Triangle and call the functions.
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);
#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;
}
}