首页 > 解决方案 > 头文件 C++ 中的数组问题

问题描述

我正在尝试创建一个具有自己的领土数组的播放器对象,但是每当我编译这些错误时,就会出现:

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 ';'

我尝试将数组移动到 cpp 文件,但随后数组在 toDefend 方法中变得未定义。

这是我的 cpp 文件到目前为止的样子:

 #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;
            }
        }

我的 h 文件看起来像

#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

帮助将不胜感激

标签: c++

解决方案


在您的头文件中,移动class Territory {class Card{声明上面class Player {,因为它正在使用它们。


推荐阅读