I have a Deck
class which is a container for 52 Card
objects. Deck
is derived from another class called CardCollection
(since I want similar groups of Cards elsewhere that are not a full deck of cards). My Problem is that I can create a Deck
object using
Deck deck();
But when I use
Deck deck = Deck();
Clang-tidy (in CLion) complains that Candidate constructor (the implicit copy constructor) not viable: expects an l-value for 1st argument
. My understanding (based on this question was that these two ways of instantiating were basically the same, but since one causes warnings
I'll only paste the constructors of these class declarations to prevent this beign a "wall-o-text" question.
//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);
//...
and
// 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();
//...
};
For starters this
Deck deck();
is a function declaration that has no parameters and has the return type Deck
.
Secondly the copy constructor of the class CardCollection
CardCollection(CardCollection &other);
can not bind the non-constant reference to a temporary object.
Declare it like
CardCollection( const CardCollection &other);