Deck.h

#ifndef DECK_H
#define DECK_H

class Deck
{
public:
	struct card{int symbol; int value;};
	card card_list[52];
	void generate_deck();
	void main();
private:
	static int const diamond = 1;
	static int const club = 2;
	static int const heart = 3;
	static int const spade = 4;
};

#endif

Deck.cpp

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <time.h>

#include "Deck.h"

using namespace std;
void Deck::generate_deck()
{
	for(int array_location=0;array_location<=12;array_location++)
	{
		card_list[array_location].symbol = diamond;
		card_list[array_location].value = array_location+1;
	}
}

void Deck::main()
{
	generate_deck();
}

I get an access violation error when I try to compile and the compiler will just crash.

However, everything would work fine if I made the declaration for card card_list[52] inside the class instead of the header.


*Note : the code above is just a small part of the whole program.

So the code you pasted works? Since card_list is in the class.
+ are you sure the _compiler_ is crashing?

Please give us the smallest source that reproduces the problem.. including all files that we need (e.g. the card class etc.) - then we can give better suggestions.

Sorry for any misunderstanding but what I meant was if the "struct card" and "card card_list[52]" was declared in the class, it would work.

However, both the "struct card" and "card card_list[52]" is declared in the header.(I need it in the header so that I'll be able to access the struct from a different class).

I got the access violation error when I tried running the code in debug mode(Visual Studio Express 2010). If I tried to build(ctrl + F5), the whole command prompt would just crash.

So it compiles then while it's running it crashes, correct?

OOPS! Sorry for the mistake, I somehow got confused with compiling and running.

Yeah, it compiles just fine but it crashes while running.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.