Hello. I'm trying to develop a very simple user-based information system. For example, read a folder for .ini files and get the pInfo stuff for each different player. It's for a network game I'm developing.

So here's a BASIC example.

if( PlayerInfo[playerOne][pLeader] == 1 )
{
    cout << "Player 1 is the leader of Team 1.";
}

However, things aren't going as I planned. This is my code.

/* Main.cpp

   2D Array Test */

//Include & Namespace
#include <iostream>
using namespace std;

//Define's
#define MAX_PLAYERS 32

//playerInfo enum
enum pInfo
{
	pLevel = 0,
	pMember = 0,
	pLeader = 0,
};
int PlayerInfo[MAX_PLAYERS][pInfo];

//Main process
int main()
{

	return 0;
}

Here's the errors I get.

1>Compiling...
1>Main.cpp
1>c:\documents and settings\aleksander\my documents\visual studio 2008\projects\2d array test\2d array test\main.cpp(16) : error C2144: syntax error : 'pInfo' should be preceded by ']'
1>c:\documents and settings\aleksander\my documents\visual studio 2008\projects\2d array test\2d array test\main.cpp(16) : error C2144: syntax error : 'pInfo' should be preceded by ';'
1>c:\documents and settings\aleksander\my documents\visual studio 2008\projects\2d array test\2d array test\main.cpp(16) : error C2143: syntax error : missing ';' before ']'
1>c:\documents and settings\aleksander\my documents\visual studio 2008\projects\2d array test\2d array test\main.cpp(16) : error C2059: syntax error : ']'

Is this kind of thing even possible?

Recommended Answers

All 3 Replies

the problem you are having is on line 19. when you declare a 2d array you declare the rows and columns of the array. your statement int PlayerInfo[MAX_PLAYERS][pInfo]; is saying define an array of 32 rows and pInfo columns. since pInfo is a type and not a value you can not use it in this way. if you want to crate an array of pIfos you could use

pInfo players[MAX_SIZE];

this would create an array of 32 pInfos types. if this isn't what your looking for let me know.

Why not use

enum pInfo
{
	pLevel,  // =0
	pMember, // =1
	pLeader   // =2
};


int PlayerInfo[MAX_PLAYERS][3];

otherwise you have all the elements in the enum = 0 so PlayerInfo[n][pLeader] would be the same as PlayerInfo[n][pLevel] (all accessing the [n][0] cell). The latter change is going along with what NathanOliver said, but I'll add since you know the number of elements in the enum ahead of time there's no reason not to declare your 2D array as such.

Excellent! Thank you so much, jonsca. That makes so much sense, too.

Here's the code what I have now. And it works flawlessly.

/* Main.cpp

   2D Array Test */

#include <iostream>
using namespace std;

#define MAX_PLAYERS 32

enum pInfo
{
    pLevel,  //0
    pMember, //1
    pLeader  //2
};
int PlayerInfo[MAX_PLAYERS][3];

int main()
{
	PlayerInfo[1][pLevel] = 15;
	PlayerInfo[15][pLeader] = 5;

	int tmpInfo = PlayerInfo[1][pLevel];
	int tmpInfoTwo = PlayerInfo[15][pLeader];
	int tmpInfoThree = PlayerInfo[15][pMember];
	int tmpInfoFour = PlayerInfo[12][pMember];

	cout << tmpInfo << endl << endl;
	cout << tmpInfoTwo << endl;
	cout << tmpInfoThree << endl << endl;
	cout << tmpInfoFour;

	int asdf;
	cin >> asdf;
	return 0;
}

Now I just need to create a linked list of 'playerid's to my game.. Thanks a lot to both of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.