如何在c++中移动对象的向量

q9yhzks0  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(188)

我是c++编程的新手,我想制作一个21点游戏,希望能让逻辑工作起来,然后用一些代码制作一个图形版本。
我的代码如下所示,Visual Studio声明未发现任何问题,但当我尝试编译代码时,出现以下错误:
严重性代码说明项目文件行抑制状态错误C2672“交换”:未找到匹配的重载函数blackJack C:\程序文件\Microsoft Visual Studio\2022\社区\VC\工具\MSVC\14.34.31933\包含\实用程序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;
}

如果这是一个非常基本的错误,请提前道歉,但由于我的调试技能也很基本,所以似乎无法找出它。

bkhjykvo

bkhjykvo1#

如果你查看错误信息的上下几行,编译器应该知道程序中哪一行导致了一连串的错误:

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);

第76行是对std::shuffle的调用。

const int numberOfDecks = 1;

你的card类有一个非静态的const成员。这会自动从类中移除默认的赋值和移动操作符。你不能像这样默认地将一个card赋值给另一个,因为这意味着--根据定义--一个对象的const成员会被另一个对象的成员的值神秘地替换。正如Spock先生所说:“这不合逻辑”。
您需要删除const类成员,或者为card类定义自己的operator=重载,该重载使用您的card类执行任何对=有意义的操作。

q8l4jmvw

q8l4jmvw2#

谢谢大家的回答。他们真的很有帮助-我有很多东西要学,但我发现它很有趣!
你的答案第一次就起作用了。我一删除const类成员,程序就编译无误了。
我也听取了大家对类设计的意见,我提出了下面的解决方案,它非常有效。现在我将在处理函数等方面获得一些乐趣。
再次感谢。

#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;
}

相关问题