Jump to content
  • Advertisement
Sign in to follow this  
Grain

overloading the [] operator

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having some trouble overloading the [] operator.
if(Input[FIRE]) // I get a compile error: conditional expression of type 'InputMap' is illegal
{
    ....
}

//so I tried 
bool KeyPressed = Input[FIRE] // And I get : cannot convert from 'InputMap' to 'bool'


if(Input->operator [](FIRE)) // this works however
{
    ....
}



Input is of Type InputMap Which alows me to map keys to spesific game inputs like "FIRE" or "JUMP" ect.. Here is my InputMap class
class InputMap
{
    int Inputs[9];
    bool Values[9];
public:
    void MapKey(int key, int mapIndex)
    {
        if(key < 255 && key > 0 && mapIndex < 9 && mapIndex > 0)
            Inputs[mapIndex] = key;
    }
    void SetValue(bool Value, int key)
    {
        if(key < 255 && key > 0)
        {
            for(int i = 0; i < 9; ++i)
                if(Inputs == key)
                    Values = Value;
        }
    }
    bool GetValue(int mapIndex)
    {
        if(mapIndex < 9 && mapIndex > 0)
            return Values[mapIndex];
        return false;
    }
    bool operator[](int mapIndex)
    {
        return GetValue(mapIndex);
    }
};



Share this post


Link to post
Share on other sites
Advertisement
Is 'Input' a a pointer to an instance your InputMap? If so you can not use the '[]' operator on it. The '[]' operator in this case makes the compiler think you want to access an InputMap object from an array of InputMap objects.

You'll either have to use the alternate syntax or deference the pointer first:

if((*Input)[FIRE])
{
\\...
}


That or keep a reference to the instance of your InputMap object instead of a pointer to it.

arm.

Share this post


Link to post
Share on other sites
It looks like Input is an InputMap *, in which case operator[] works as it always does for pointers and returns *(pointer + offset). You cannot overload operator[] for pointer types. So just use the third syntax.

By the way, is there any particular reason you've written your own map call instead of just typedef std::map< int, bool > InputMap;?

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by arm
Is 'Input' a a pointer to an instance your InputMap? If so you can not use the '[]' operator on it. The '[]' operator in this case makes the compiler think you want to access an InputMap object from an array of InputMap objects.

You'll either have to use the alternate syntax or deference the pointer first:

if((*Input)[FIRE])
{
\\...
}


That or keep a reference to the instance of your InputMap object instead of a pointer to it.

arm.


Ok Thanks. I'll just have to use a regular InputMap in stead of a pointer to one.


Quote:
Original post by Enigma
By the way, is there any particular reason you've written your own map call instead of just typedef std::map< int, bool > InputMap;?

Well I need the "Input[]" notation because I originally was using an unmapped simple array of bools to get inputs. But I wanted to allow the user to configure the keys them selves. The problem was that I had about a hundred "Input[]" throughout my code and needed to keep that notation. And I was unsure if std::map accesses in that way. Also I'm not entirely certain how std::map<int,bool> would work. As I am not mapping int data to bool data, I am mapping one index of bools to another index of bools. Although now that I think about it I suppose could have used std::map<int,int> inside my class… I think.

Share this post


Link to post
Share on other sites
Ah, sorry, I misread your code. I thought it was a plain map. I didn't realise that your key is a keyboard key, not a key-value pair key! So what you really need is an array and a map:
#include <map>
#include <vector>

class InputMap
{

public:

enum Action
{
Fire,
Up,
// etc
numberOfActions
};

InputMap()
:
inputKeyMapping_(numberOfActions)
{
}

void mapInputKey(int inputKey, Action action)
{
inputKeyMapping_[action] = inputKey;
if (userInput_.find(inputKey) == userInput_.end())
{
userInput_[inputKey] = false;
}
}

void setUserInput(int inputKey, bool value)
{
userInput_[inputKey] = value;
}

bool lookup(Action action)
{
return userInput_[inputKeyMapping_[action]];
}

bool operator[](Action action)
{
return lookup(action);
}

private:

std::vector< int > inputKeyMapping_;
std::map< int, bool > userInput_;

};


Enigma

Share this post


Link to post
Share on other sites
and this is why I won't use a language without type safety! Your statement here:
"Input is of Type InputMap"
is false! It is of type InputMap*

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!