samedi 5 novembre 2022

C++ Scope, unqualified-id before 'public' [closed]

Hi this is a homework for a CS course. It includes 3 Classes Team, Player, LeagueManagementSystem. There are interactions set within the classes. However I am getting some errors namely, expected unqualified-id before 'public' /'private' and datafield was not declared in this scope. I cannot resolve these problems

#ifndef LEAGUEMANAGEMENTSYSTEM_H
    #define LEAGUE_MANAGEMENTSYSTEM_H

    #include <string>
    #include "Team.h"
    using namespace std;

    class LeagueManagementSystem {
        public:
            LeagueManagementSystem();
            ~LeagueManagementSystem();
            void addTeam( const string name, const int year );
            void removeTeam( const string name );
            void addPlayer( const string teamName, const string playerName,
            const int jersey, const int salary );
            void removePlayer( const string teamName, const string playerName );
            void transferPlayer( const string playerName,
            const string departTeamName, const string arriveTeamName );
            void showAllTeams() const;
            void showTeam( const string name ) const;
            void showPlayer( const string name ) const;
        private:
            Team* teams;
            int teamSize;
    };
    #endif // LEAGUEMANAGEMENTSYSTEM_H
    #ifndef TEAM_H
    #define TEAM_H

    #include <string>
    #include "Player.h"

    using namespace std;

    struct Team;
    struct Player;

    class Team{
    public:
        Team(string, int);
        Team();
        ~Team();
        string getName() const;
        int getFoundationYear() const;
        void addPlayer(Player);
        Player* removePlayer(string); // pass the player name to check
        Player* getPlayer(string) const;
        int getPlayersLength() const;
        int getTotalSalary() const;
    private:
        string name;
      Player* players;
        int playersLength = 1;
       int playersLastIndex = 0;
      int foundationYear = 0;
    };
    #endif // TEAM_H
    #ifndef PLAYER_H
    #define PLAYER_H
    #include "Team.h"
    #include <string>

    using namespace std;

    class Player{
    public:
        Player(string, string, int, int);
        Player();
        ~Player();
        void setTeamName(string);
        void setName(string);
        void setJersey(int);
        void setSalary(int);
        string getTeamName() const;
       string getName() const;
       int getJersey() const;
      int getSalary() const;
     string toString() const;

    private:
        string teamName;
        string name;
        int jersey;
        int salary;
    };

#include "Team.h"
    #include <string>
    //class Team{
    private:
            string name;
            Player* players = new Player[1];
            int playersLength = 1;
            int playersLastIndex = 0; //for players array last index
            int foundationYear = 0;
    public:
            Team::Team(string n, int fy): name(n), foundationYear(fy){

            }
            Team::Team(){

            }
            Team::~Team(){

            }
            string Team::getName() const{
                return name;
            }
            int Team::getFoundationYear() const{
                return foundationYear;
            }
            void Team::addPlayer(Player player){
                if(playersLastIndex >= playersLength){
                    //double the size of players array
                    playersLength *= 2;
                    Player* newPlayers = new Player[playersLength];
                    //copy elements
                    for(int i = 0; i < playersLength; i++ ){
                        newPlayers[i] = players[i];
                    }
                    delete[] players; // may need to remove !!!
                    players = newPlayers;
                }
                players[++playersLastIndex] = player;
            }
            Player* Team::removePlayer(string name){
                Player* returnValue = nullptr;
                for(int i = 0; i < playersLength; i++){
                    if(players[i].getName() == name){
                        returnValue = &players[i];
                        //players[i] = nullptr;
                        //shift elements
                        for(int j = i; j < playersLastIndex; j++){
                            players[j] = players[j + 1]; // might be problematic !!!
                        }
                        playersLastIndex--;
                        return returnValue;
                    }
                }
                return returnValue;
            }
            Player* Team::getPlayer(string name) const{ // MAY NEED TO REMOVE CONST
                Player* returnValue = nullptr;
                for(int i = 0; i < playersLength; i++){
                    if(players[i].getName() == name){
                        returnValue = &players[i];
                    }
                }
                return returnValue;
            }
            int Team::getPlayersLength() const{
                return playersLength;
            }
            int Team::getTotalSalary() const{
                int sum = 0;
                for(int i = 0; i < playersLength; i++){
                    sum += players[i].getSalary();
                }
                return sum;
            }


    ```


League management class which represents the league management system
    ```
    #include "LeagueManagementSystem.h"
    #include "Player.h"
    #include "Team.h"
    #include <string>
    #include <iostream>

    using namespace std;

    private:
        Team* teams = new Team[1];
        int teamSize = 1;
        int teamLastIndex = 0; // last index of the last team

    public:
            LeagueManagementSystem::LeagueManagementSystem(){

            }
            LeagueManagementSystem::~LeagueManagementSystem(){

            }
            void LeagueManagementSystem::addTeam(const string name, const int year){
                Team newTeam(name, year);
                if(teamLastIndex >= teamSize){
                //double the size of players array
                teamSize *= 2;
                Team* newTeams = new Team[teamSize]; // Might be problematic !!!
                //copy elements
                for(int i = 0; i < teamSize; i++ ){
                    newTeams[i] = teams[i]; // might be problematic !!!
                }
                delete[] teams; // may need to remove !!!
                teams = newTeams; // swap pointers
                }
                teams[++teamLastIndex] = newTeam;
            }
            void removeTeam( const string name ){
                Team* returnValue = nullptr;
                for(int i = 0; i < teamSize; i++){
                    if(teams[i].getName() == name){
                        returnValue = teams[i];
                        teams[i] = nullptr;
                        //shift elements
                        for(int j = i; j < teamLastIndex; j++){
                            teams[j] = teams[j + 1]; // might be problematic !!!
                        }
                        teamsLastIndex--;
                        //return returnValue;
                    }
                }
            //return returnValue;
            }
            void addPlayer( const string teamName, const string playerName,
            const int jersey, const int salary ){
                Player player(teamName, playerName, jersey, salary);
                //find the corresponding team
                for(int i = 0; i < teamSize; i++){
                    if(teams[i].getName() == teamName){
                        teams[i].addPlayer(player);
                        break;
                    }
                }
            }
            void removePlayer( const string teamName, const string playerName ){
                for(int i = 0; i < teamSize; i++){
                    if(teams[i].getName() == teamName){
                        teams[i].removePlayer(playerName);
                        break;
                    }
                }
            }
            void transferPlayer( const string playerName,
            const string departTeamName, const string arriveTeamName ){
                Player transferPlayer;
                for(int i = 0; i < teamSize; i++){
                    if(teams[i].getName() == departTeamName){
                        transferPlayer = teams[i].removePlayer(playerName);
                    }
                }
                for(int i = 0; i < teamSize; i++){
                    if(teams[i].getName() == arriveTeamName){
                        teams[i].addPlayer(transferPlayer);
                    }
                }

            }
            void showAllTeams(){
                cout << endl;
                cout << "Teams in the league management system:" << endl;
                for(int i = 0; i < teamSize; i++){
                    Team curr = teams[i];
                    cout << curr.getName() << ", " << curr.getFoundationYear() << ", " << curr.getPlayersLength() << " players, " << curr.getTotalSalary() << endl;
                }
            }
            void showTeam( const string name ){
                for(int i = 0; i < teamSize; i++){
                    Team curr = teams[i];
                    if(curr.getName() == name){
                        cout << curr.getName() << ", " + curr.getFoundationYear() << ", " + curr.getPlayersLength() << " players, " << curr.getTotalSalary() << endl;
                    }
                }
            }
            void showPlayer( const string name ){
                for(int i = 0; i < teamSize; i++){
                        Player* curr = teams[i].getPlayer(name);
                    if(curr->getName() == name){
                     cout << curr->toString();
                    }
             }
         }
    ```

    ```

    ```
    #include <string>
    #include "Player.h"
    #include "Team.h"
    class Player{
    private:
        string name;
        int jersey;
        int salary;
        string teamName;

    public:
        Player::Player(string tn, string n, int j, int s): teamName(n), name(n), jersey(j), salary(s){

        }
        Player::Player(){

        }
        Player::~Player(){

        }
      void Player::setTeamName(string tn){
       teamName = tn;
      }
       void Player::setName(string n){
            name = n;
      }
        void Player::setJersey(int j){
            jersey = j;
     }
        void Player::setSalary(int s){
            salary = s;
        }
        string Player::getTeamName() const{
            return teamName;
        }
        string Player::getName() const{
            return name;
        }
        int Player::getJersey() const{
            return jersey;
        }
     int Player::getSalary() const{
            return salary;
     }
        string Player::toString() const{
            return name + (" " + jersey) + (" " + salary) + " " + teamName;
      }
    ```

Mostly these code is about a homework in which it includes 3 classes. I haven't included the header files but you can guess them from the class itself. Here is the error page of my code.
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Aucun commentaire:

Enregistrer un commentaire