j76goatboy
j76goatboy

Reputation: 445

C++ How to declare map variable

I'm new to C++ and having trouble declaring a map variable in a header file. In various posts I've seen people include #include <map> and it fixed whatever their problem was so I included that in the file.

#include <map>

class Game
{

    typedef void (Game::*InputResponse)( void );

public:

    Game();

private:

    std::map <char[], InputResponse> inputResponseMap;
};

When I compile and build, it points to the #include "Game.h" of the .cpp file for the class. In file included from Game.cpp:8: Removing the map type variable inputResponseMap fixes the problem so I assume that is where the problem is. What am I doing wrong?

Upvotes: 2

Views: 2384

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126967

char[] is an incomplete type, and in general C-style arrays lack several properties that are required for map key types (in particular, C arrays cannot be assigned and cannot be compared directly through operator <).

Just use std::string as key type.

Upvotes: 2

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

std::map <char[], InputResponse> inputResponseMap;

The char[] part of this is not a valid (complete) C++ type.

If your intent is for your map's key to be a text string, you should use std::string:

std::map <std::string, InputResponse> inputResponseMap;

And, of course #include <string>, for this.

Upvotes: 4

Related Questions