c++ - 头文件 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
帮助将不胜感激
解决方案
在您的头文件中,移动class Territory {
和class Card{
声明上面class Player {
,因为它正在使用它们。
推荐阅读
- go - 从 DDD 的角度来看,如何处理 DB 特定类型?
- angular - 是否可以在角度材料表版本 11 中进行内容投影?
- java - 使用 RowSorter 和 RowFilter 搜索 JTable 的相同代码仅适用于 4 个 JTable 中的 2 个
- python - 我想将图像从我的计算机发送到服务器
- c# - 如何设置 Masstransit Order 状态机?
- hibernate - 如何正确配置 hibernate.c3p0.debugUnreturnedConnectionStackTraces
- scala - 如何使用具有组合列的案例类从 RDD 创建数据集
- javascript - toLocaleDateString 给出不同的结果
- arrays - Google Sheet Query - 动态构建参考数组
- javascript - 上传的图像在 html 网站上显示为图像图标