候选构造函数(隐式复制构造函数)不可行:需要第一个参数的左值

Candidate constructor (the implicit copy constructor) not viable: expects an l-value for 1st argument

问题

我有一个 Deck class,它是 52 个 Card 对象的容器。 Deck 派生自另一个名为 CardCollection 的 class(因为我想要其他地方的类似纸牌组,而不是一副完整的纸牌)。我的问题是我可以使用

创建一个 Deck 对象
Deck deck();

但是当我使用

Deck deck = Deck();

Clang-tidy(在 CLion 中)抱怨 Candidate constructor (the implicit copy constructor) not viable: expects an l-value for 1st argument。我的理解(基于 ​​ 的问题是这两种实例化方式基本相同,但由于其中一种会导致警告

代码

我将只粘贴这些 class 声明的构造函数,以防止出现“wall-o-text”问题。

//Card.h

    class Card {
    public:
        int rank; 
        basic_string<char> suit;

        Card(int r, std::string s); // rank and suit

        ~Card();

    //...
}

// CardCollection.h
#include <vector>
#include "Card.h"


class CardCollection {

protected:
    vector<Game::Card> _cards;
public:
    CardCollection();
     ~CardCollection();
     CardCollection(CardCollection &other);
     explicit CardCollection(int n);
     explicit CardCollection(vector<Game::Card> &cards);
    //...

// Deck.h

#include "Card.h"
#include <vector>
#include "CardCollection.h"

class Deck : public CardCollection {
public:
    Deck();
    ~Deck();
    explicit Deck(vector<Game::Card> &cards);
    Deck * shuffle();
    //...
};


对于初学者这个

Deck deck();

是一个函数声明,没有参数并且具有 return 类型 Deck

其次是class CardCollection

的复制构造函数
CardCollection(CardCollection &other);

无法将非常量引用绑定到临时对象。

像这样声明

CardCollection( const CardCollection &other);