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
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.
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:
Line 76 is the call to
std::shuffle
.Your
card
class has a non-staticconst
member. That automatically removes the default assignment and move operators from the class. You can’t assign onecard
to another, by default, just like that, because that would mean — by definition — that one object’sconst
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 ownoperator=
overload for thecard
class that does whatever makes sense for=
to do, with yourcard
classes.