是的,這是作業,但我不知道如何克服這個錯誤。我必須使用一個班來創建和洗牌一副牌,然後輸出他們的套裝和價值。當我第一次運行程序時彈出錯誤。任何幫助,將不勝感激!C++訪問衝突寫入位置0x00000000
這是卡類:
#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
}
這是我的主要功能:
#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
請使用[MCVE](https://stackoverflow.com/help/mcve) –
'Card * deck [decknum] = {};'這個定義了一個52'Card *'指針的數組,全部初始化爲' NULL'。 –
'if(m_Suit = 1)'將始終爲真。改用==代替。 – vsoftco