无法创建将 std::array<T, n> 作为类成员的构造函数

Cannot create constructor with std::array<T, n> as class member

本文关键字:gt 成员 构造函数 array 创建 std lt      更新时间:2023-10-16

在这里代码

#include "card.h"
#include <array>
constexpr int DECK_SIZE = 52;
class Deck {
    std::array<Card, DECK_SIZE> m_deck; 
public:
    Deck();
    ~Deck() = default;
    Card getCard(int index) { return m_deck[index]; }
};

卡只是另一个类,在这里无关。我想立即创建甲板构造函数,它将仅创建52个卡对象并将其分配给我的数组。虽然,当我去deck.cpp并开始这样做

#include "deck.h"
Deck::Deck() {    
}

我有错误的错误,即我引用已删除的函数

'std :: array :: array(void)':尝试引用已删除的 功能

我不太了解,数组大小是在编译时已知的,现在我只想创建对象并将其分配给此数组,为什么我不能这样做?

在这种情况下,卡没有意识到卡是非常相关的,无论如何是代码

#include <SFML/Graphics.hpp>
constexpr auto FILE_PATH = "res\cards\";
class Card {
    char m_rank;
    char m_suit;
    int m_value;
    sf::RectangleShape m_shape;
    sf::Texture m_texture;
public:
    Card(char, char, int, std::string);
    ~Card() = default;
    char getRank() { return m_rank; }
    char getSuit() { return m_suit; }
    int getValue() { return m_value; }
    void setValue(int p_value) { m_value = p_value; }
    sf::Texture getTexture() { return m_texture; }
    sf::RectangleShape getShape() { return m_shape; }
    void setPosition(float p_x, float p_y) { m_shape.setPosition(p_x, p_y); }
};
Card::Card(char p_rank, char p_suit, int p_value, std::string p_texture_file_name)
    :m_rank(p_rank), m_suit(p_suit), m_value(p_value) {
    m_texture.loadFromFile(FILE_PATH + p_texture_file_name);
    m_shape.setSize((sf::Vector2f(70, 90)));
    m_shape.setTexture(&m_texture);
}

为了默认构建std::array<Card, DECK_SIZE>Card必须默认构造。您的Card类不是。