GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Mar-2005, 19:46
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road
Question

Help using const arrays in c++ classes


I have looked all over the place for the answer. How do you handle const in c++ classes?
Here is my class.
CPP / C++ / C Code:
#ifndef CARDDECK_H
#define CARDDECK_H

const int numCards = 52; // total number of cards
const int numSuits = 4;  // total number of suits

const char *suit[ numSuits ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 
const char *face[ numCards ] = 	
{ "Ace", "Deuce", "Three", "Four",
  "Five", "Six", "Seven", "Eight",
  "Nine", "Ten", "Jack", "Queen", "King"};

class CardDeck {

public:

	int deck[ numSuits ][ 13 ];											// array of cards
	
	CardDeck();																	// constructor
	~CardDeck();																// deconstructor
	
	void initDeck();
	void shuffle( int [][ 13 ] );												// shuffle deck of cards
	void deal( const int [][ 13 ], const char *[], const char *[] );			// deal deck of cards

}; // end CardDeck class

#include "CardDeck.cpp"
#endif
----
If I declare the *suit and *face pointer arrays as data members in the class, I get an error about static const stuff. How should I go about this? I have no idea?
  #2  
Old 30-Mar-2005, 20:08
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
I guess, its because you have not initialized all the members in array "face". It is supposed to have 52 members strings, but you have initialized only 13.

If you are not sure in advance as to how many member array will have, you can have the following

CPP / C++ / C Code:

const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 
const char *face[] =   
{ "Ace", "Deuce", "Three", "Four",
  "Five", "Six", "Seven", "Eight",
  "Nine", "Ten", "Jack", "Queen", "King"};
	

This compiled fine for me. You can add member later as the array grows.

Thanks,
  #3  
Old 30-Mar-2005, 20:25
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road
Quote:
Originally Posted by nkhambal
I guess, its because you have not initialized all the members in array "face". It is supposed to have 52 members strings, but you have initialized only 13.

If you are not sure in advance as to how many member array will have, you can have the following

CPP / C++ / C Code:

const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 
const char *face[] =   
{ "Ace", "Deuce", "Three", "Four",
  "Five", "Six", "Seven", "Eight",
  "Nine", "Ten", "Jack", "Queen", "King"};
	

This compiled fine for me. You can add member later as the array grows.

Thanks,

This is true, but my question is. How do you define a const array data member in the public: area of the class. As you see.

If i want to define
CPP / C++ / C Code:
const int = 10;
in the public: of a class I have to do it like this
CPP / C++ / C Code:
static const int = 10;
For some reason you can't do that with a pointer array?!?

Also, for you solution for not defining the size of the array, so the array can grow. That won't work b/c the array is const and you can't edit a const.
  #4  
Old 30-Mar-2005, 20:34
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road

This is how I want it


This is how I would like it, so I can't create an object of the class and access the data members in the class.

CPP / C++ / C Code:
#ifndef CARDDECK_H
#define CARDDECK_H

class CardDeck {

public:

	static const int numCards = 52;											// total number of cards
	static const int numSuits = 4;											// total number of suits

	const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 		// array of suit names
	const char *face[] = { "Ace", "Deuce", "Three", "Four",
	  					   "Five", "Six", "Seven", "Eight",
	 					   "Nine", "Ten", "Jack", "Queen", "King"};
	int deck[ numSuits ][ 13 ];												// array of cards
	
	CardDeck();																// constructor
	~CardDeck();															// deconstructor
	
	void initDeck();
	void shuffle( int [][ 13 ] );											// shuffle deck of cards
	void deal( const int [][ 13 ], const char *[], const char *[] );		// deal deck of cards

}; // end CardDeck class

#include "CardDeck.cpp"
#endif

This is the error I get
Code:
error: ISO C++ forbids initializaiton of member 'suit' error: making 'suit' static error: invalid in-class initialization of static data member no-integral type 'const char*[0]'

I get the same error for 'face'
  #5  
Old 30-Mar-2005, 20:48
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Sorry, but I do not know about C++ syntax. I thought it was just a syntax related problem with "const char**" which got solved when I complied as a simple C program.

Hang on there, someone else might reply to your post.

Thanks,
  #6  
Old 31-Mar-2005, 02:16
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road

Found solution


This is what I was trying to accomplish and I have fixed the problem!

http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/Thinking_in_C++/tic0111.html
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error - unresolved external symbol _WinMain ap6118 C++ Forum 6 23-Mar-2005 23:46
Help with const? jheron C++ Forum 4 10-Mar-2005 21:24
I need help implementing kjc_13 C++ Forum 0 14-Feb-2005 17:00
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Error: (function) undeclared -first use of this function crystalattice C++ Forum 6 01-Nov-2004 05:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.