简体   繁体   中英

C++ Access violation writing location 0x00000000

Yes, this is homework, but I have no idea how to get past this error. I have to use a class to create and shuffle a deck of cards, then output their suits and values. The error pops up when I first run the program. Any help would be appreciated!

This is the card class:

#include "Card.h"

void Card::Set(int suit, int value)
{
m_Suit = suit;
m_Value = value;
//set the appropriate member variable
}
string Card::GetSuit()
{
if (m_Suit = 0){
    return "Spades";
}
else if (m_Suit = 1){
    return "Hearts";
}
else if (m_Suit = 2){
    return "Diamonds";
}
else if (m_Suit = 3){
    return "Clubs";
}
//returns suit based on suit variables's number
}

string Card::GetValue()
{
if (m_Value = 0){
    return "Ace";
}
else if (m_Suit = 1){
    return "2";
}
else if (m_Suit = 2){
    return "3";
}
else if (m_Suit = 3){
    return "4";
}
else if (m_Suit = 4){
    return "5";
}
else if (m_Suit = 5){
    return "6";
}
else if (m_Suit = 6){
    return "7";
}
else if (m_Suit = 7){
    return "8";
}
else if (m_Suit = 8){
    return "9";
}
else if (m_Suit = 9){
    return "10";
}
else if (m_Suit = 10){
    return "Jack";
}
else if (m_Suit = 11){
    return "Queen";
}
else if (m_Suit = 12){
    return "King";
}
//return value based on value variable's number
}

this is my main function:

#include <iostream>
#include <string>
#include <time.h>
#include "Card.h"

using namespace std;

void RandomizeSeed();
int RandomRange(int min, int max);
void SwapCards(Card* a, Card* b);
//function declarations

int main()
{
RandomizeSeed();
//for a random number later
const int decknum = 52;
//# of cards in a deck
Card *deck[decknum] = {};
//a deck of card classes...?

for (int i = 0; i <= decknum; i++){
    float suit = i/13;
    float value = i%13;

    deck[i]->Set((suit), (value));
    //assign value to each card of the deck
}

for (int i = 0; i <= decknum*2; i++){
    SwapCards(deck[RandomRange(0, 51)], deck[RandomRange(0, 51)]);
    //shuffle cards

}

for (int i = 0; i <= decknum; i++){
    cout << deck[i]->GetSuit() << "of "<< deck[i]->GetValue() << endl;
//output the shuffled deck
}


for (int i = 0; i <= decknum*1; i++){
    delete deck[i];
    deck[i] = nullptr;
    //delete the cards

}

system("pause");
return 0;
}

void SwapCards(Card* a, Card* b)
{
Card temp = *a;
*a = *b;
*b = temp;
}//swap 2 cards to shuffle

void RandomizeSeed()
{
srand(time(NULL));
}

int RandomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
} //random number function

Some errors have been pointed out by others in the comments. I will add this one:

const int decknum = 52;
Card *deck[decknum] = {};
for (int i = 0; i <= decknum; i++){

Here you declare an array of 52 elements. The valid indices go from 0 to 51, not to 52, so the proper for loop to go through it is:

for (int i = 0; i < decknum; i++){

that is, using < instead of <= . With <= you will access the array out of its bound, causing undefined behaviour, for example working with wrong data or even crashing the program.

And, as Igor said, you have an array of pointers that do not point to an object. For that, you have to use new . I just wanted to add that it's easy to tell that there's an error because at the end of the program you are calling delete , but you have never called new .

And the error found by vsoftco (which I hadn't seen) indicates you are not reading compiler warnings, or that they are disabled. Please enable them and read them. It's a very good habit even for experienced programmers.

You never initialise the cards.

Card *deck[decknum] = {};

will just initialise the deck to 0. Any time you call Set on one of the entries you will get an write exception. Though you do have the delete each card loop.

So you need to add in an allocation to the initialisation loop, just after the line above:

for (int i = 0; i < decknum; i++){ // < not <=
    deck[i] = new Card(); //<-- add this line

    float suit = i/13;
    float value = i%13;

    deck[i]->Set((suit), (value));
}

Also as pointed out by vsoftco, all of your if statements in the Set and Get methods should be == not =.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM