我无法访问数组中元素的数据成员。我有一个类型为gamePiece的数组,它是我在单独的标题中定义的类。 gamePiece有一个字符串数据成员“name”,我需要从我的gameBoard文件中的一个函数设置,gameBoard是gamePieces的二维数组。每当我尝试运行主程序时,它调用“readBoard”,当它试图为其中一个gamePieces分配一个“名称”时它会停止。我究竟做错了什么?
//gamePiece.h file
#ifndef GAMEPIECE_H_INCLUDED
#define GAMEPIECE_H_INCLUDED
#include <string>
using namespace std;
class gamePiece //not a struct b/c there are other functions not included here
{
public:
string name;
int row;
}
//gameBoard.h
class gameBoard
{
public:
gamePiece board[][6];
int foxCount, sheepCount;
void readBoard(istream& boardFile);
//void printBoard();
};
//gameBoard.cpp
void gameBoard::readBoard(istream& boardFile)
{
int tFoxCount = 0;
int tSheepCount = 0;
gamePiece tBoard[6][6];
gamePiece tPiece;
tboard[0][0].name = "Sherry";
cout << tboard[0][0].name;
}