Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;类中向量随机洗牌的运行时错误_C++_Class_Vector_Blackjack - Fatal编程技术网

C++ c++;类中向量随机洗牌的运行时错误

C++ c++;类中向量随机洗牌的运行时错误,c++,class,vector,blackjack,C++,Class,Vector,Blackjack,我正在尝试编写一个21点游戏的代码。我在空闲时间一直在自学C++,这是我第一次在任何网站上发布关于编程的文章。 我一直在寻找我的问题的答案,并且学到了很多。。但是这个问题完全把我难住了。我担心我在完成任务时完全错了,希望你能在这方面帮助我 我有一个卡片类和一个包含52张卡片向量的卡片组类。vector是Deck类的私人成员,我担心这是我的问题 当我向代码中添加random_shuffle行时,它可以很好地编译,但随后控制台窗口崩溃(Windows7x64,code::blocks,c++)。我不

我正在尝试编写一个21点游戏的代码。我在空闲时间一直在自学C++,这是我第一次在任何网站上发布关于编程的文章。 我一直在寻找我的问题的答案,并且学到了很多。。但是这个问题完全把我难住了。我担心我在完成任务时完全错了,希望你能在这方面帮助我

我有一个卡片类和一个包含52张卡片向量的卡片组类。vector是Deck类的私人成员,我担心这是我的问题

当我向代码中添加random_shuffle行时,它可以很好地编译,但随后控制台窗口崩溃(Windows7x64,code::blocks,c++)。我不知道我做错了什么。我调用向量随机访问迭代器begin()和end()

deck.h

#ifndef DECK_H
#define DECK_H

#include <vector>

using namespace std;

/** Card Class */
class Card
{
public:
/** Constructor prototypes */
//Card(); //default constructor
Card(int s, int r) : suit(s), rank(r) {} 

/** GET function prototypes */
int getRank(); // returns card number as int
string getSuit(); // returns the suit in a string

private:
int rank;
int suit;
} ;

/** Deck class */
class Deck
{
public:
Deck();
vector <Card> get_deck() { return deck; };

private:
vector<Card> deck;
};

#endif // DECK_H
#如果没有甲板#
#定义甲板
#包括
使用名称空间std;
/**卡片类*/
班级卡
{
公众:
/**构造器原型*/
//Card();//默认构造函数
卡牌(整数s,整数r):西装(s),等级(r){}
/**获取函数原型*/
int getRank();//将卡号返回为int
string getSuit();//以字符串形式返回西装
私人:
整数秩;
内服;
} ;
/**甲板级*/
甲板
{
公众:
甲板();
向量get_deck(){return deck;};
私人:
向量甲板;
};
#完//DECK_H
deck.cpp

#include <iostream>
#include <string>
#include <vector>
#include "deck.h"

using namespace std;

/** Deck ctor to initialise deck */
Deck::Deck()
{
for(int suit = 0; suit < 4; suit++)
{
    for(int rank = 0; rank < 13; rank++)
    {
        deck.push_back(Card(suit,rank));
    }
}

}

/** Functions to GET rank and suit */
// Function to get rank as int
int Card::getRank()
{
return rank;
}

// Function to get suit as string
string Card::getSuit()
{
switch(suit)
{
    case 0:
    return "Diamonds";

    case 1:
    return "Hearts";

    case 2:
    return "Clubs";

    case 3:
    return "Spades";

    default:
    return "Error";
}
}
#include <iostream>
#include <algorithm>
#include <ctime> // time()
#include <string>
#include <vector>

#include "deck.h"

using namespace std;

int main()
{

Deck mydeck;

random_shuffle( mydeck.get_deck().begin(), mydeck.get_deck().end() );

// Loop to iterate through deck of cards
for(int i = 0; i<52; i++)
{
    cout << mydeck.get_deck()[i].getRank() << " of " << mydeck.get_deck()[i].getSuit() << endl;
}

// Display size of deck
//cout << endl << "The size of deck is: " << mydeck.get_deck().size() << endl;


return 0;
}
#包括
#包括
#包括
#包括“deck.h”
使用名称空间std;
/**甲板驾驶员初始化甲板*/
甲板
{
对于(int suit=0;suit<4;suit++)
{
for(int-rank=0;rank<13;rank++)
{
扑克牌。推回(牌(套装,军衔));
}
}
}
/**获得军衔和军衔的功能*/
//函数将秩设为int
int Card::getRank()
{
返回等级;
}
//函数将西服作为字符串获取
字符串卡::getSuit()
{
开关(套装)
{
案例0:
归还“钻石”;
案例1:
回归“心”;
案例2:
返回“俱乐部”;
案例3:
返回“黑桃”;
违约:
返回“错误”;
}
}
main.cpp

#include <iostream>
#include <string>
#include <vector>
#include "deck.h"

using namespace std;

/** Deck ctor to initialise deck */
Deck::Deck()
{
for(int suit = 0; suit < 4; suit++)
{
    for(int rank = 0; rank < 13; rank++)
    {
        deck.push_back(Card(suit,rank));
    }
}

}

/** Functions to GET rank and suit */
// Function to get rank as int
int Card::getRank()
{
return rank;
}

// Function to get suit as string
string Card::getSuit()
{
switch(suit)
{
    case 0:
    return "Diamonds";

    case 1:
    return "Hearts";

    case 2:
    return "Clubs";

    case 3:
    return "Spades";

    default:
    return "Error";
}
}
#include <iostream>
#include <algorithm>
#include <ctime> // time()
#include <string>
#include <vector>

#include "deck.h"

using namespace std;

int main()
{

Deck mydeck;

random_shuffle( mydeck.get_deck().begin(), mydeck.get_deck().end() );

// Loop to iterate through deck of cards
for(int i = 0; i<52; i++)
{
    cout << mydeck.get_deck()[i].getRank() << " of " << mydeck.get_deck()[i].getSuit() << endl;
}

// Display size of deck
//cout << endl << "The size of deck is: " << mydeck.get_deck().size() << endl;


return 0;
}
#包括
#包括
#包括//时间()
#包括
#包括
#包括“deck.h”
使用名称空间std;
int main()
{
甲板我的甲板;
随机洗牌(mydeck.get_deck().begin(),mydeck.get_deck().end());
//循环遍历一副牌
对于(int i=0;i请尝试从
get\u deck()
返回
vector&
。在发布的代码中,您将制作两个单独的副本并返回它们

当随机洗牌尝试完成它的工作时,它的迭代器指向两个不同的向量

正如@Will在对另一个答案的评论中指出的,最好通过实现一个方法
void Deck::shuffle()
来保留封装,该方法在成员
Deck
上调用
random\u shuffle
,并且根本不公开
Deck

此访问器方法:

vector <Card> get_deck() { return deck; };
但是,这允许调用者修改内部数组,这通常不是一个好主意。为了避免这种情况,您应该通过
const
reference返回它:

const vector <Card>& get_deck() { return deck; }
const vector&get_deck(){return deck;}

但是如果你这样做了,那么
std::random\u shuffle
就不能修改数组。因此,要解决这个问题,理想的解决方案是在
Deck
类中添加一个class方法,该类本身调用
random\u shuffle

+1用于发布重现问题的可编译代码,-1用于不缩小问题范围。所以0。你的Deck class i在我看来,一个牌组类应该有自己的shuffle方法,即
void Deck::shuffle(){random_shuffle(Deck.begin(),Deck.end());}
--以及其他方法,如获取顶牌或底牌,或从牌组中随机获取一张牌。然后我将摆脱你的
获取牌组()
method.@StarPilot这是指向此页面的链接。LOL。很抱歉。抓错了窗口的URL。就在其中的一天。@LuchianGrigore将来我会将代码缩小到我的问题所在……谢谢。另一个选择是在Deck类本身中实现shuffle。@这是一个更好的主意吗?谢谢你的帮助告诉我哪里出了问题,并帮助我解决问题。现在一切都有意义了,我现在可以看到封装过程。我怎么能循环通过向量中的实际卡片而不是向量的副本呢?我猜这是通过Deck的成员函数实现的,比如
void Deck::displayCard()
然后哪个将访问私有变量(向量组)?