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 Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\utility 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.
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.