Search code examples
c++game-enginesfml

Constructor of class does not match the argument list....but it does....?


I'm making a chess engine and using this code:

#pragma once

    #include "SFML\Graphics.hpp"
    #include "Grid.h"

    enum PieceType { //place gamePieces into Grid not field, cus grid is already in field
        W_BISHOP,
        W_PAWN,
        W_KING,
        W_QUEEN,
        W_ROOK,
        W_KNIGHT,
        B_BISHOP,
        B_PAWN,
        B_KING,
        B_QUEEN,
        B_ROOK,
        B_KNIGHT
    };

class GamePieces //TODO PUT TEAM IN GRID, ACCESS GAME PIECES IN GRID TO MOVE THEM
{
public:
    GamePieces(){}
    GamePieces(PieceType& type, Coords& position) : m_type(type),m_position(position) {
        switch (type) {
            case W_BISHOP:
                m_gamePiece.loadFromFile("w_bishop");
                m_gamePieceSprite.setTexture(m_gamePiece);
                break;

           //REST OF CASES OF PIECETYPE ARE PRETTY MUCH THE SAME

    }
    ~GamePieces();
private:
    sf::Texture m_gamePiece;
    sf::Sprite m_gamePieceSprite;
    Coords m_position;
    PieceType m_type;
};



enum TeamColor {
    BLACK,
    WHITE
};

struct Team {
    //16 pieces in regular chess
    //map that assigns specific game pieces coordinates

    GamePieces m_chessPieces[16];
    TeamColor color;

    Team() {}

    Team(TeamColor& c) {
        Coords coord;

        switch (c) {
            case WHITE: 

                for (int i = 0; i < 8; i++) {
                    coord.m_x += 52.5;
                    coord.m_y += 52.5;
                    m_chessPieces[i] = GamePieces(PieceType::B_PAWN, coord);
                }
                break;

Cut out parts I didn't need but basically the error occurs in this line:

GamePieces(PieceType::B_PAWN, coord); 

and it says the constructor of GamePieces doesn't have the specified argument list but it does!


Solution

  • This is because you attempt to assign an rvalue (PieceType::B_PAWN) to a non-const reference. The language doesn't allow you to do this.

    The solution is to make the constructor take by value or const reference:

    GamePieces(PieceType type, const Coords& position) //...