-5

I am very new to C++ and my programming assignment ask to shuffle a deck of cards. So far I have been able to create a vector and add all 52 cards into the deck and display them. However when I try to swap the elements I get errors. Please help me!

Card header file:

     #include <string>
    using namespace std;

    #ifndef CARD_H
    #define CARD_H

    class Card {

      public:
        Card();
        Card(int f, int s);
        string toString();
        const int numFace = 13;
        const int numSuit = 4;
        const static string faces[];
        const static string suits[];
        void setFace(int face);
        void setSuit(int suit);
        void display();

      private:
        int face, suit;

    };

    #endif

Card c++ file:

    #include <iostream>
    #include <string>
    #include <stdexcept>
    #include "Card.h"
    using namespace std;

    const string Card::faces[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    const string Card::suits[] = {"Diamonds", "Clubs", "Hearts", "Spades"};

    Card::Card(){
        setFace(1);
        setSuit(1);
    }

    Card::Card(int face, int suit){
        setFace(face);
        setSuit(suit);
    }

    void Card::display(){
        cout << faces[face-1] + " of " + suits[suit-1] << endl;
    }

    void Card::setFace(int face){
        if(face >= 1 && face <= 13){
            this->face = face;
        } else {
            throw invalid_argument("face must be 1-13!");
        }
    }

    void Card::setSuit(int suit){
        if(suit >= 1 && suit <= 4){
            this->suit = suit;
        } else {
            throw invalid_argument("suit must be 1-4!");
        }
    }

    string Card::toString(){
        return faces[face-1] + " of " + suits[suit-1] + "\n";
    }

DeckOfCards header file:

      #include <string>
    #include <vector>
    #include "Card.h"
    using namespace std;

    #ifndef DECKOFCARDS_H
    #define DECKOFCARDS_H

    class DeckOfCards {

      public:
        const static int totalCards;
        DeckOfCards(); 
        void shuffle();
        string dealCard();
        bool moreCards();
        vector< Card > deck;
        void initCards();
        void display();

      private:

        int currentCard;

    };

    #endif

DeckOfCard cpp file:

#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <ctime>
#include "Card.h"
#include "DeckOfCards.h"
#include <cstdlib>
#include <ctime>
#include <utility> 
using namespace std;

DeckOfCards::DeckOfCards(){
    vector<Card> deck;
    initCards();
}

void DeckOfCards::initCards(){
    const  int numCardInSuit = 13;

    for(int i = 1 ; i <= numCardInSuit; i++){
       deck.push_back(Card(i,1)); //push diamonds
       deck.push_back(Card(i,2)); //push clubs
       deck.push_back(Card(i,3)); //push hearts
       deck.push_back(Card(i,4)); //push spades
    }
}

void DeckOfCards::shuffle(){
    srand( static_cast<unsigned int>( time(0)) );
    int randomNum = 0;

    Card card;
    for(int i = 0; i < deck.size()-1; i++){
        cout << "entered swap" << endl;

        randomNum = rand() % 52;
        swap(deck[i], deck[randomNum]);

        /* vector< int > vect;        
           vect.push_back(99);       Creating a vector and 
           vect.push_back(1);        swapping this way works!
           for(int ele : vect){
               cout << ele << endl;
           }

           swap(vect[0], vect[1]);
           cout << "Swapped" << endl;
           for(int ele : vect){
            cout << ele << endl;
           }
        */

       /*
        card = deck[i]
        deck[i] = deck[randomNum];
        deck[randomNum] = card;
        */
        cout << "finished swap" << endl;
    }
}


string DeckOfCards::dealCard(){
    return deck[1].toString();
}

bool DeckOfCards::moreCards(){
   return true;
}

void DeckOfCards::display(){
    for(int i = 0; i < deck.size(); i++){
        cout << deck[i].toString();
    }
}

Heres the main:

#include <iostream>
#include "DeckOfCards.h"


using namespace std;

int main()
{
    DeckOfCards myDeck;
    myDeck.display();
    myDeck.shuffle();

    return 0;
}

Error when program is compiled:

In file included from /usr/include/c++/5/bits/stl_pair.h:59:0,
                 from /usr/include/c++/5/bits/stl_algobase.h:64,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from DeckOfCards.cpp:1:
/usr/include/c++/5/bits/move.h: In instantiation of 'void std::swap(_Tp&, _Tp&) [with _Tp = Card]':
<span class="error_line" onclick="ide.gotoLine('DeckOfCards.cpp',40)">DeckOfCards.cpp:40:32</span>:   required from here
/usr/include/c++/5/bits/move.h:186:11: error: use of deleted function 'Card& Card::operator=(Card&&)'
       __a = _GLIBCXX_MOVE(__b);
           ^
In file included from DeckOfCards.cpp:6:0:
Card.h:7:7: note: 'Card& Card::operator=(Card&&)' is implicitly deleted because the default definition would be ill-formed:
 class Card {
       ^
Card.h:7:7: error: non-static const member 'const int Card::numFace', can't use default assignment operator
Card.h:7:7: error: non-static const member 'const int Card::numSuit', can't use default assignment operator
In file included from /usr/include/c++/5/bits/stl_pair.h:59:0,
                 from /usr/include/c++/5/bits/stl_algobase.h:64,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from DeckOfCards.cpp:1:
/usr/include/c++/5/bits/move.h:187:11: error: use of deleted function 'Card& Card::operator=(Card&&)'
       __b = _GLIBCXX_MOVE(__tmp);
           ^

Sorry for the long post I just wanted to fit everything in to make it easy for anyone to help! Feel free to critique my code! Thanks so much!

PS I am compiling the program using https://www.onlinegdb.com/online_c++_compiler

1 Answers1

0

To pick out some key lines from you error

In instantiation of 'void std::swap(_Tp&, _Tp&) [with _Tp = Card]':

use of deleted function 'Card& Card::operator=(Card&&)'

'Card& Card::operator=(Card&&)' is implicitly deleted because the default definition would be ill-formed:

non-static const member 'const int Card::numFace', can't use default assignment operator

You are trying to use std::swap with your cards.
Internally that will try to use operator=, meaning

Cards a, b;
a = b; // Here we are using the operator=

So the error message goes on to say, use of deleted function operator=
It is implicitly deleted because the default definition would be ill-formed.
It is ill formed because you have const ints that can't be assigned to in your class.

const int c = 5;
c = 10; // This is illegal for a const

The error message also hint at a solution by saying non-static const member ...

You can solve this is a number of ways, where making the const int be static seems like a good option in your case.

super
  • 12,335
  • 2
  • 19
  • 29