Question

I have been trying to use a std::pair in a class constructor. Whenever I try to declare an object or use this constructor I get the error that "no instance of constructor matches the argument list. Here is the code:

playAi::playAi(pair<int, int> p, int x, int y, int piece_pos[])
{
    int xPos = x;
    int yPos = y;
    setup();
}

playAi::~playAi(void)
{
}

void playAi::setup()
{
    pair<int, int> column1(10, 110);
    pair<int, int> column2(130, 230);
    pair<int, int> column3(250, 250);
    int colX1 = 10;
    int colY1 = 130;
    //initializing the pair
    playAi temp[] = {
        **playAi(column1, colX1, colY1, piece_pos[0])**,
    };
}

I cannot think of any other way that I can declare the object than what I am doing. The last entry "piece_pos[]" is inherited from another class. Do I need to use the full scoped name? Thanks for the help.

Was it helpful?

Solution

The problem is not the pair - your code is fine in that regard (although you can avoid creating a temporary by using make_pair template function, this is merely an optimization, not a fix).

The problem is that the constructor expects to see a pointer to piece_pos or an array of piece_pos, while you are attempting to pass piece_pos[0] to it. You cannot create an array "inline" in a function call like this:

piece_pos tmp[1];
...
playAi(column1, colX1, colY1, tmp)

Here is how you can rewrite this code with make_pair:

playAi(make_pair(10, 110), colX1, colY1, tmp)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top