Click to See Complete Forum and Search --> : map array problem
Denion
December 9th, 2004, 05:21 AM
okay I am making an array of maps but i get the following error at compile time:
graph.h:18: error: parse error before `[' token
#include <map>
using namespace std;
class graph
{
public:
struct edge
{
int cost;
char *left;
char *right;
};
graph(char * fileName);
private:
map<char *, int> nodeMap;
nodeMap nodeList [1000];//the problem is here ***********************
bool isCycle(char * One, char * Two);
bool downList();
int knownCounter;
};
anyone got ideas?
marten_range
December 9th, 2004, 05:38 AM
1. This looks like a header file. Please don't put using namespace std; in a header file. Make the world a better place and say no to 'using' declarations in header files.
2. Assuming that by char* you mean a string. Why use char* when there is the excellent class std::string? std::map<char*, int> will not behave correctly if you use char* (as std::map<> will compare the pointer values not the string values).
3. The problem in your case is most likely that you forgot to put a typedef before map<char*,int>. If you do that it works better.
So a "fixed" version could look like this:
#include <map>
#include <string>
class graph
{
public:
typedef std::map<std::string, int> nodeMap;
struct edge
{
int cost;
std::string left;
std::string right;
};
graph(std::string fileName);
private:
nodeMap nodeList [1000];
bool isCycle(std::string const & One, std::string const & Two);
bool downList();
int knownCounter;
};
Hope this helps.
Andreas Masur
December 9th, 2004, 06:11 AM
map<char *, int> nodeMap;
The above declares a class member variable named 'nodeMap' of type 'std::map<char *, int>' and not a type....use 'typedef' as indicated...
Axter
December 9th, 2004, 10:20 AM
To add to Andreas Masur's comment,
Modify nodeMap to a typedef.
Example:
typedef map<char *, int> nodeMap;
nodeMap nodeList [1000];
However, although this will compile, I don't think this is really what you want in your code.
Why are you trying to make an array of 1000 nodeMap?
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.