无法创建将 std::array<T, n> 作为类成员的构造函数
Cannot create constructor with std::array<T, n> as class member
在这里代码
#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
类不是。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时