CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004
    Posts
    4

    map array problem

    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
    Code:
    #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?

  2. #2
    Join Date
    Sep 2004
    Posts
    519

    Re: map array problem

    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:

    Code:
        #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.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: map array problem

    Quote Originally Posted by Denion
    Code:
    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...

  4. #4
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: map array problem

    To add to Andreas Masur's comment,
    Modify nodeMap to a typedef.
    Example:
    Code:
    	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?
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured