类的构造函数与参数列表不匹配....但它确实......?

Gia*_*tti 3 c++ game-engine sfml

我正在制作一个国际象棋引擎并使用这个代码:

#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;
Run Code Online (Sandbox Code Playgroud)

切出我不需要的部分,但基本上错误发生在这一行:

GamePieces(PieceType::B_PAWN, coord); 
Run Code Online (Sandbox Code Playgroud)

并且它说构造函数GamePieces没有指定的参数列表但它确实如此!

Tar*_*ama 5

这是因为您尝试将rvalue(PieceType::B_PAWN)分配给非const引用.该语言不允许您这样做.

解决方案是使构造函数通过值或const引用:

GamePieces(PieceType type, const Coords& position) //...
Run Code Online (Sandbox Code Playgroud)