0

I am trying to create a player object that has its own array of Territories but whenever I compile these errors appear :

Player.cpp(15,4): error C2065: 'playerTerritories': undeclared identifier
Player.cpp(27,11): error C2065: 'playerTerritories': undeclared identifier
Player.h(15,29): error C3646: 'playerTerritories': unknown override specifier
Player.h(15,29): error C2143: syntax error: missing ',' before '['
Player.h(15,32): error C2143: syntax error: missing ')' before ';'
Player.h(15,32): error C2238: unexpected token(s) preceding ';'

I have tried moving the array to the cpp file but then the array becomes undefined in the toDefend method.

This is how my cpp file looks so far:

 #include "Player.h"
    #include <iostream>
    
    using std::cout;
    
        //Arrays for territories to choose from
        extern std::string territoriesArr[] = { "Alaska","North West Territories", "Quebec", "Alberta", "Ontario", "Eastern US", "Western US", "Mexico", "Venezuela", "Peru", "Brazil", "Argentina" };
        //The territories of our enemy
        extern std::string enemy_territoriesArr[] = { "Great Britain", "Iceland", "Northern Europe", "Western Europe", "Southern Europe", "Scandanavia" };
        //The array of cards that we can draw
        extern std::string cardTypeArr[] = { "bomb", "reinforcement", "blockade", "airlift", "diplomacy" };
        
        Player::Player() {
            //Randomly choose territories and place into our playerTerritories array, we dont care about dupilcates for now
            for (int i = 0; i < 6; i++) {
                int r = rand() % 12;
                    playerTerritories[i] = Territory(territoriesArr[r]);    
            }
        
            //Randomly choose cards and place into our card array
            for (int i = 0; i < 3; i++) {
                int r = rand() % 5;
                playerHand[i] = Card(cardTypeArr[r]);
            }
        }
        
        //Method to print out all the territories of a player
        void Player::toDefend() {
            cout << "Territories to defend: ";
                for (int i = 0; i < 6; i++) {
                cout << playerTerritories[i].getTerritoryName() << std::endl;
            }
        }
        
        //Method to print out all the enemy territories 
        void Player::toAttack() {
            cout << "Territories to attack";
            for (std::string x : enemy_territoriesArr) {
                cout << x << std::endl;
            }
        }
        
        void Player::getPlayerHand() {
            cout << "Cards in hand";
            for (Card x : playerHand) {
                cout << x.getCardName() << std::endl;
            }
        }

and my h file looks like

#ifndef PLAYER_H
#define PLAYER_H

#include <string>

class Player {
public:
    Player();
    void toDefend();
    void toAttack();
    //void issueOrder(std::string order); TODO latter
    void getPlayerHand();
    //void getOrderList(); TODO latter
private:
    Territory playerTerritories[6];
    Card playerHand[3];
};

class Territory {
public:
    Territory();
    Territory(std::string name);
    std::string getTerritoryName();
private:
    std::string territoryName;
};

class Card{
public: 
    Card();
    Card(std::string type);
    std::string getCardName();
private:
    std::string cardType;
};

class Order {
public:
    Order(std::string name);
private:
    std::string orderName;
};
#endif

Help would be much appreciated

2
  • 1
    At this line Territory playerTerritories[6]; the type Territory is not known yet. Although I would expect a different error message. Same goes for Card playerHand[3]; Commented Oct 12, 2020 at 18:23
  • From where Territory came ? Add the header of it in ur .h Commented Oct 12, 2020 at 18:25

1 Answer 1

1

In your header file, move class Territory { and class Card{ declarations above class Player {, as it is using them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.