【问题标题】:Constructor of class does not match the argument list....but it does....?类的构造函数与参数列表不匹配......但它......?
【发布时间】:2016-11-20 17:42:22
【问题描述】:

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

#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;

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

GamePieces(PieceType::B_PAWN, coord); 

它说GamePieces 的构造函数没有指定的参数列表,但它有!

【问题讨论】:

    标签: c++ game-engine sfml


    【解决方案1】:

    这是因为您尝试将右值 (PieceType::B_PAWN) 分配给非常量引用。该语言不允许您这样做。

    解决办法是让构造函数取值或者const引用:

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

    【讨论】:

      【解决方案2】:

      临时不能绑定到非 const 引用,将你的构造函数更改为

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

      GamePieces(PieceType type, const Coords& position)
      

      【讨论】:

        【解决方案3】:

        您的构造函数无缘无故地通过非常量引用获取其参数。

        非常量引用不能绑定到常量,例如PieceType::B_PAWN

        您应该按值传递枚举,除非您想修改被调用函数中的参数。您应该通过 const 引用传递诸如 Coord 之类的结构,如果它们足够小且足够简单,则有时通过值传递(但通过 const 引用是一种安全的后备选项)。

        【讨论】:

          【解决方案4】:

          您的功能似乎是错误的。删除引用:

          GamePieces(PieceType type, Coords position)
          

          更新: 请注意,上面的行是解决问题的最简单方法。正如之前的 cmets 所说,您可以通过值或 const 引用来获取参数。

          在这种情况下,第一个是非常小的对象,因此您可以按值获取。第二个可以通过 cont 引用获取,因为它可能更大(8 个字节?)。从这个意义上说,@TartanLlama 的答案是完美的。

          尽管如此,按值取值是一个很好的解决方案,因为您的方法是内联的,并且编译器有机会优化代码(例如,使用寄存器而不是堆栈来传递参数)。

          【讨论】:

            【解决方案5】:

            语法:

             GamePieces(PieceType::B_PAWN, coord);
            

            不正确。对于enum 变量,您不必声明枚举类型。正确的语法如下:

             GamePieces(B_PAWN, coord);
            

            声明:

            GamePieces(PieceType& type, Coords& position)
            

            不正确,因为您使用了非常量引用。您必须将其更改为以下内容:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-09-09
              • 2021-07-02
              • 2016-03-05
              • 2017-07-18
              • 2018-05-04
              • 1970-01-01
              • 2013-02-15
              • 2013-10-29
              相关资源
              最近更新 更多