skip to Main Content

I’m new to c++ programming and trying to make a blackjack game with a view of getting the logic working and then using some of the code to make a graphical version.

My code is below and Visual Studio states no issues found, but when I try and compile the code, I get the following error:

Severity Code Description Project File Line Suppression State
Error C2672 ‘swap’: no matching overloaded function found blackJack C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.34.31933includeutility 78



#include <iostream>
#include <string>
#include <map>
#include <string_view>
#include <vector>
#include <algorithm>
#include <random>
#include <iterator>
#include <cstdlib>
#include <utility>

class card
{
public:
    std::string suite;
    char pictureCard = 'n';
    int value = 0;
    const int numberOfDecks = 1;
    void setCardValue(const int& value, std::string suite, char pictureCard);
    void getDeck();
    void setDeck(const int& numberOfDecks);

    void shuffle();
    std::vector <card> v;
};

void::card::setCardValue(const int& value, std::string suite, char pictureCard)
{
    this->value = value;
    this->suite = suite;
    this->pictureCard = pictureCard;
}

void::card::setDeck(const int& numberOfDecks) 
{
    card c1;
    for (int n = 0; n < numberOfDecks; n++)
    { 
        int j = 2;
        for (int i = 0; i < 9; i++) //set Numbered cards
        {
            c1.setCardValue(j, "Hearts", 'N');
            v.push_back(c1);
            c1.setCardValue(j, "Clubs", 'N');
            v.push_back(c1);
            c1.setCardValue(j, "Diamonds", 'N');
            v.push_back(c1);
            c1.setCardValue(j, "Spades", 'N');
            v.push_back(c1);
            j++;
        }

        for (int p = 0; p < 4; p++) //set Pictured cards
        {
            char N = 'N';
            if (p == 0) { N = 'J'; }
            else if (p == 1) { N = 'Q'; }
            else if (p == 2) { N = 'K'; }
            else if (p == 3) { N = 'A'; };

            c1.setCardValue(10, "Hearts", N);
            v.push_back(c1);
            c1.setCardValue(10, "Clubs", N);
            v.push_back(c1);
            c1.setCardValue(10, "Diamonds", N);
            v.push_back(c1);
            c1.setCardValue(10, "Spades", N);
            v.push_back(c1);
        }
    }

    int seed = 1;
    std::default_random_engine e(seed);


    std::shuffle(v.begin(), v.end(), e);


}

void card::getDeck()
{
    for (auto it = begin(v); it != end(v); ++it)
    {
        std::cout << it->value << it->suite << it->pictureCard << std::endl;
    }
    std::cout << v.size() << std::endl;


}



int main()
{
    card c2;
    c2.setDeck(6);
    c2.getDeck();

    return 0;
}

Apologies in advance if this is a really basic error, but can’t seem to figure it out as my debugging skills are basic as well.

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone for the responses. They were all really helpful - I have a lot to learn but I'm finding it fun!

    Sam Varshavchik your answer worked first time. As soon as I removed the const class member the program compiled without error.

    I have also taken everyone's comments regarding class design on board and I have come up with the following solution below that works perfectly. I will now have some fun working on dealing functions etc.

    Many thanks again.


    #include <iostream>
    #include <string>
    #include <map>
    #include <string_view>
    #include <vector>
    #include <algorithm>
    #include <random>
    #include <iterator>
    #include <cstdlib>
    #include <utility>
    
    
    class card
    {
    public:
        std::string suite;
        char pictureCard = 'n';
        int value = 0;
        void setCardValue(const int& value, std::string suite, char pictureCard);
    };
    
    void::card::setCardValue(const int& value, std::string suite, char pictureCard)
    {
        this->value = value;
        this->suite = suite;
        this->pictureCard = pictureCard;
    }
    
    
    class deck
    {
    public:
        const int numberOfDecks = 1;
        void setDeck(const int& numberOfDecks);
        std::vector <card> v;   
    };
    
    void::deck::setDeck(const int& numberOfDecks)
    {
        card c1;
        for (int n = 0; n < numberOfDecks; n++)
        {
            int j = 2;
            for (int i = 0; i < 9; i++) //set Numbered cards
            {
                c1.setCardValue(j, "Hearts", 'N');
                v.push_back(c1);
                c1.setCardValue(j, "Clubs", 'N');
                v.push_back(c1);
                c1.setCardValue(j, "Diamonds", 'N');
                v.push_back(c1);
                c1.setCardValue(j, "Spades", 'N');
                v.push_back(c1);
                j++;
            }
    
            for (int p = 0; p < 4; p++) //set Pictured cards
            {
                char N = 'N';
                if (p == 0) { N = 'J'; }
                else if (p == 1) { N = 'Q'; }
                else if (p == 2) { N = 'K'; }
                else if (p == 3) { N = 'A'; };
    
                c1.setCardValue(10, "Hearts", N);
                v.push_back(c1);
                c1.setCardValue(10, "Clubs", N);
                v.push_back(c1);
                c1.setCardValue(10, "Diamonds", N);
                v.push_back(c1);
                c1.setCardValue(10, "Spades", N);
                v.push_back(c1);
            }
        }
    }
    
    
    class dealer
    {
    public:
        void shuffle(deck &deck);
        void getDeck(deck &deck);
    };
    
    void::dealer::shuffle(deck &deck)
    {
        auto e = std::default_random_engine{};
        std::shuffle(deck.v.begin(), deck.v.end(), e);
    }
    
    void::dealer::getDeck(deck &deck)
    {
        for (auto it = begin(deck.v); it != end(deck.v); ++it)
        {
            std::cout << it->value << it->suite << it->pictureCard << std::endl;
        }
        std::cout << deck.v.size() << std::endl;
    }
    
    
    int main()
    {
        deck deck;
        dealer dealer;
        deck.setDeck(6);
        dealer.shuffle(deck);
        dealer.getDeck(deck);
    
        return 0;
    }
    

  2. If you look a few lines above and below the error message your compiler should tell which line in your program resulted in the cascade of errors:

    t.C:76:17:   required from here
    /usr/include/c++/12/bits/stl_algobase.h:182:11: error: no matching function for call to ‘swap(card&, card&)’
      182 |       swap(*__a, *__b);
    

    Line 76 is the call to std::shuffle.

     const int numberOfDecks = 1;
    

    Your card class has a non-static const member. That automatically removes the default assignment and move operators from the class. You can’t assign one card to another, by default, just like that, because that would mean — by definition — that one object’s const member gets mysteriously replaced by another object’s member’s value. As Mr. Spock would say: "this is not logical".

    You’ll either need to remove the const class member or define your own operator= overload for the card class that does whatever makes sense for = to do, with your card classes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search