我收到了奇怪的错误,不知道该怎么办。我试图找到解决方案,但找不到适合我的解决方案。这是纸牌游戏,当我尝试将纸牌添加到我的列表(一副纸牌)中时,会显示错误。这是我的全部代码,以完全理解我的想法。它是用C语言编写的,但是我使用的是C ++中的cin / cout,这就是为什么我有“使用命名空间标准”的原因。谢谢你的帮助 :)
#define SIZE 52
#define HALF_SIZE 26
using namespace std;
typedef enum Suit { Diamonds, Hearts, Spades, Clubs } Suit_t;
typedef enum Power { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace } Power_t;
typedef struct card {
Suit_t suit;
Power_t power;
}cards;
typedef struct deck {
cards* p_deck;
int size;
}decks;
void Deck_Creator(cards* deck) {
for (int i = 0; i < SIZE; i++) {
deck[i].suit = (Suit)(i / 13);
deck[i].power = (Power)(i % 13);
}
}
void Display_Deck(cards* deck) {
for (int i = 0; i < SIZE; i++) {
if (deck[i].suit == 0) {
cout << "Diamond ";
}
else if (deck[i].suit == 1) {
cout << "Heart ";
}
else if (deck[i].suit == 2) {
cout << "Spade ";
}
else {
cout << "Club ";
}
cout << " " << deck[i].power << endl;
cout << endl;
}
}
void Deck_Shuffler(cards* deck) {
int changer = 0; // Index of changed card
cards temp = {}; // Temporary holding place for changing
srand(time(NULL)); // Function to get random numbers with current time
for (int i = 0; i < SIZE; i++) {
changer = rand() % SIZE; // Generating random number from 0 to 51
temp = deck[i];
deck[i] = deck[changer]; // if changer = i
deck[changer] = temp;
}
}
void Deck1_Creator(cards* deck, cards* deck1, decks* q_deck1) {
for (int i = 0; i < HALF_SIZE; i++) {
memcpy(&deck1[i], &deck[i], sizeof(cards)); // Copying content using memcpy function
}
q_deck1->p_deck = deck1;
q_deck1->size = HALF_SIZE;
}
void Deck2_Creator(cards* deck, cards* deck2, decks* q_deck2) {
int i = HALF_SIZE;
for (int i = 26; i < SIZE; i++) {
deck2[i - HALF_SIZE] = deck[i]; // Simple copying struct array content into another array
}
q_deck2->p_deck = deck2;
q_deck2->size = HALF_SIZE;
}
void table_creator(decks* q_deck) {
q_deck->p_deck = {};
q_deck->size = 0;
}
void enqueue(decks* q_deck, cards card) {
// I TRIED THAT TOO
//int n;
//n = q_deck->size;
q_deck->p_deck[q_deck->size] = card;
q_deck->size++;
}
cards* dequeue(decks* q_deck) {
cards tmp_card;
tmp_card = q_deck->p_deck[0];
for (int i = 0; i < q_deck->size - 1; i++) {
q_deck->p_deck[i] = q_deck->p_deck[i + 1];
}
q_deck->size--;
return &tmp_card;
}
void disp_queue(decks* q_deck) {
for (int i = 0; i < q_deck->size; i++) {
cout << q_deck->p_deck[i].suit <<" ";
cout << q_deck->p_deck[i].power << endl;
}
}
int main() {
cards deck[SIZE];
cards deck1[SIZE];
cards deck2[SIZE];
cards table1[HALF_SIZE];
cards table2[HALF_SIZE];
decks q_deck1;
decks q_deck2;
decks q_table1;
decks q_table2;
char choice;
table_creator(&q_table1);
table_creator(&q_table2);
for(; ;) { //Never ending loop
cin >> choice;
switch (choice) {
case '1':
cout << endl << "Unshuffled deck: " << endl;
Deck_Creator(deck);
Display_Deck(deck);
break;
case '2':
cout << endl << "Shuffled deck: " << endl;
Deck_Shuffler(deck);
Display_Deck(deck);
break;
case '3':
cout << endl << "Hand 1:" << endl;
Deck1_Creator(deck, deck1, &q_deck1);
disp_queue(&q_deck1);
break;
case '4':
cout << endl << "Hand 2:" << endl;
Deck2_Creator(deck, deck2, &q_deck2);
disp_queue(&q_deck2);
break;
case '5':
cards tmp_card = *dequeue(&q_deck1);
enqueue(&q_table1, tmp_card);
tmp_card = *dequeue(&q_deck2);
enqueue(&q_table2, tmp_card);
disp_queue(&q_table1);
disp_queue(&q_table2);
break;
case '6':
disp_queue(&q_deck1);
break;
}
}
return 0;
}
错误仅在我按下开关功能中的情况5时弹出。其余的工作正常。
ERROR弹出此函数:
cards* dequeue(decks* q_deck) {
cards tmp_card;
tmp_card = q_deck->p_deck[0]; // EXACTLY IN THIS LINE RED X OCCURS
for (int i = 0; i < q_deck->size - 1; i++) {
q_deck->p_deck[i] = q_deck->p_deck[i + 1];
}
q_deck->size--;
return &tmp_card;
}
请帮助我理解此错误,以便我完全知道原因以及此错误的工作方式:(
感谢您的所有宝贵时间,我知道这是很长的代码。
最佳答案
Yano关于分配给p_deck的内存不足是正确的。我很惊讶其他人都在工作(您似乎根本没有在这里为卡分配任何空间,相反,您只是将卡值写入指针旁边的区域,从技术上讲这并不是属于您的指针,并不能保证保持不变)。
您可以通过在构造中添加构造函数/析构函数来解决此问题,因为您使用的是C ++。
typedef struct deck {
cards* p_deck;
int size;
//constructor
deck() : size(SIZE) //this sets the size to your size constant
{
p_deck = malloc(size * sizeof(*p_deck)); //this gives p_deck memory space
}
//destructor
~deck()
{
if(p_deck) delete[] p_deck; //this clears up the memory assigned to p_deck
p_deck = nullptr;
}
}deck; //edited so it matches the actual struct name
若要使用构造函数,请在创建对象时使用
deck varName = deck();
而不是deck varName;
。当对象超出范围时(在main()或其本地函数的末尾),将自动调用析构函数。如果您出于任何原因要释放内存(例如,如果您想重新创建一个全新的卡座),也可以使用
varName.~deck();
手动调用它。关于c++ - 引发的Visual Studio异常:写访问权限冲突。 q_deck-> p_deck为0x110112。 C语言,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59167250/