Array Problems


namco

Member
Joined
Mar 22, 2006
Messages
410
Age
42
Location
Manchester, UK
Website
www.stupendous-stuff.com
I'm getting a bunch of compiler errors with:

invalid types 'int[int]' for array subscript.

They refer to all the lines below concerning boardInfo->boardNum[numOfPlayers] in the test.cpp file

CODE

void checkboard(){
if((boardInfo->boardNum[numOfPlayers] >= 31) && (boardInfo->boardNum[numOfPlayers] <= 40))
{
totdie = die1 + die2;

switch(boardInfo->boardNum[numOfPlayers])
{
case 31:
if(totdie >= 10)
{
totdie = totdie + 31;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 32:
if(totdie >= 9)
{
totdie = totdie + 32;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 33:
if(totdie >= 8)
{
totdie = totdie + 33;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 34:
if(totdie >= 7)
{
totdie = totdie + 34;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 35:
if(totdie >= 6)
{
totdie = totdie + 35;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 36:
if(totdie >= 5)
{
totdie = totdie + 36;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 37:
if(totdie >= 4)
{
totdie = totdie + 37;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 38:
if(totdie >= 3)
{
totdie = totdie + 38;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 39:
if(totdie >= 2)
{
totdie = totdie + 39;
boardInfo->boardNum[numOfPlayers] = totdie - 40;
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;

case 40:
totdie = totdie + 40;
boardInfo->boardNum[numOfPlayers] = totdie - 40;

if(boardInfo->boardNum[numOfPlayers] < 0)
{
boardInfo->boardNum[numOfPlayers] = boardInfo->boardNum[numOfPlayers] / -1;
}
break;
}
}
else
{
totdie = boardInfo->boardNum[numOfPlayers] + die1 + die2;
boardInfo->boardNum[numOfPlayers] = totdie;
}
}



It is allocated in the main function (test.cpp file) as:
CODE

boardInfo = new gameInfo[numOfPlayers];



The structure is in gameConstants.h:
CODE

int numOfPlayers = 0;

struct gameInfo
{
int boardNum;
int player;
bool turn;
};

gameInfo *boardInfo;



What am I doing wrong?

Thanks.
 
Last edited by a moderator:
boardNum is an int but you are indexing it like an array

you want

boardInfo[player]->boardNum

edit: not sure what you want boardInfo[numOfPlayers] is off the end of the array!
 
Back
Top