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 31-Mar-2005, 02:45
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road

Const Pointer with Scope Resolution Operator Issue


I am trying to do this
CPP / C++ / C Code:
const int CardDeck::numCards = 52;
const int CardDeck::numSuits = 4;

The above works fine.
Although, I can't seem to figure out how to implement
Code:
CardDeck::
with a pointer.

What I have
CPP / C++ / C Code:
const char* suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char* face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", 
						 "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

i tried this and it doesn't work.
CPP / C++ / C Code:
const char* CardDeck::suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char* CardDeck::face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", 
						 "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

Any idea how to define this properly ?

Thanks,
Bruce
  #2  
Old 31-Mar-2005, 08:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by FlipNode
Any idea how to define this properly ?

Thanks,
Bruce

The reference from your previous post gives a solution.

Here's what your header can look like:

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


class CardDeck {

public:

  int deck[ numSuits ][ 13 ];          // array of cards
  static const char *suit[ numSuits ];
  static const char *face[ numCards ];
  
  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

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

#endif

Note:

Your header included a cpp file. I strongly recommend that you don't include files with executable code. Compile them separately.


Regards,

Dave
  #3  
Old 31-Mar-2005, 12:24
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road
Quote:
Originally Posted by davekw7x
Note:

Your header included a cpp file. I strongly recommend that you don't include files with executable code. Compile them separately.


Regards,

Dave

Okay, I have taken the cpp out of the header file. My header is not the same as before. This is what it is now.

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

class CardDeck {

public:

	int deck[ 4 ][ 13 ];													// array of cards
	
	CardDeck();																// constructor
	~CardDeck();															// deconstructor
	
	void shuffle( int [][ 13 ] );											// shuffle deck of cards
	void deal( const int [][ 13 ] );										// deal deck of cards

private:

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

	const static char *suit[ 13 ];
	const static char *face[ 4 ];

	void initDeck();

}; // end CardDeck class

const int CardDeck::numCards = 52;
const int CardDeck::numSuits = 4;

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

#endif

I am getting this error.. hmm
Code:
g++ -o main main.cpp In file included from main.cpp:7: CardDeck.h:36: error: conflicting types for `const char*CardDeck::suit[4]' CardDeck.h:26: error: previous declaration as `const char*CardDeck::suit[13]' CardDeck.h:37: error: conflicting types for `const char*CardDeck::face[13]' CardDeck.h:27: error: previous declaration as `const char*CardDeck::face[4]'
  #4  
Old 31-Mar-2005, 12:39
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
The error you are seeing results from one of the most common mistaskes in programming: the silly typo/mixup. Everyone does this, it's just a matter of how often. The more careful and experienced you are, the fewer errors of this type you will have.

Take a close look at these two chucks of code copied from your post:
CPP / C++ / C Code:
const static char *suit[ 13 ];
const static char *face[ 4 ];
and
CPP / C++ / C Code:
const char *CardDeck::suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char *CardDeck::face[13] = { 
                  "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", 
                   "Eight", "Nine", "Ten", "Jack", "Queen", "King"
                  };
Do you see anything funny?

If you still don't see it (which often happens when you've been working on the same problem or looking at the same code for too long), then compare the array sizes in the first two statements with the sizes specified in the second two statements.

Don't get too frustrated about this mistake, rather try to always keep it in the front of your mind to look for these kinds of things first.

Matthew

P.S. See the first line of this post for a coincidental example of a typo!
  #5  
Old 31-Mar-2005, 13:15
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road

LOL. Gzz always learning!


Okay, now I am a little happier. I was scratching my head over that one. How do you recommend compiling this program now, seeing how I took the
CPP / C++ / C Code:
#include "CardDeck.cpp"
out of the header file.

Code:
These are the files. main.cpp , CardDeck.h , and CardDeck.cpp

Code:
I am using linux console to compile. #g++ -o main main.cpp

Here is my main.cpp file
CPP / C++ / C Code:
#include <iostream>
#include "CardDeck.h"

using namespace std;

int main( int argc, char *argv[] ) 
{
	CardDeck *pDeck = new CardDeck;					// instantiate CardDeck object

	pDeck->shuffle( pDeck->deck );					// suffle the damn deck man!
	pDeck->deal( pDeck->deck );						// give me a good hand this time! damn dealer!

	delete pDeck; 									// take shit in the tolet, ahh nice little turd.
	pDeck = 0;										// set pointer to zero after a delete

	return 0;										// flush baby! flush! como'n plllease flush
}

I get this error
Code:
g++ -o main main.cpp /tmp/ccoTQ58v.o(.text+0x2a): In function `main': : undefined reference to `CardDeck::CardDeck[in-charge]()' /tmp/ccoTQ58v.o(.text+0x6f): In function `main': : undefined reference to `CardDeck::shuffle(int (*) [13])' /tmp/ccoTQ58v.o(.text+0x81): In function `main': : undefined reference to `CardDeck::deal(int const (*) [13])' /tmp/ccoTQ58v.o(.text+0x98): In function `main': : undefined reference to `CardDeck::~CardDeck [in-charge]()' collect2: ld returned 1 exit status
  #6  
Old 31-Mar-2005, 13:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by FlipNode
Okay, now I am a little happier. I was scratching my head over that one. How do you recommend compiling this program now, seeing how I took the


Code:
These are the files. main.cpp , CardDeck.h , and CardDeck.cpp

Code:
I am using linux console to compile. #g++ -o main main.cpp


Try this (ya gotta tell the compiler about all of the files)
Quote:
g++ -o main main.cpp CardDeck.cpp

Regards,

Dave
  #7  
Old 31-Mar-2005, 13:32
FlipNode FlipNode is offline
New Member
 
Join Date: Mar 2005
Posts: 14
FlipNode is on a distinguished road
Okay I tried that and got an error but I solved it. Maybe you can explain why this has to be?

this is what I did in the console
Code:
flipnode@solid AtlantisPoker $ g++ -o main main.cpp CardDeck.cpp /tmp/ccinQecy.o(.rodata+0x0): multiple definition of `CardDeck::numCards' /tmp/ccnk0gOx.o(.rodata+0x0): first defined here /tmp/ccinQecy.o(.rodata+0x4): multiple definition of `CardDeck::numSuits' /tmp/ccnk0gOx.o(.rodata+0x4): first defined here /tmp/ccinQecy.o(.data+0x0): multiple definition of `CardDeck::suit' /tmp/ccnk0gOx.o(.data+0x0): first defined here /tmp/ccinQecy.o(.data+0x20): multiple definition of `CardDeck::face' /tmp/ccnk0gOx.o(.data+0x20): first defined here collect2: ld returned 1 exit status flipnode@solid AtlantisPoker $ g++ -o main main.cpp CardDeck.cpp flipnode@solid AtlantisPoker $ ./main King of Clubs Queen of Spades Jack of Clubs Deuce of Hearts Three of Clubs Ace of Spades Eight of Clubs Deuce of Spades Deuce of Diamonds Four of Clubs Ten of Diamonds King of Hearts Eight of Diamonds Four of Diamonds Eight of Spades Seven of Hearts Seven of Diamonds Nine of Spades Jack of Diamonds Six of Hearts King of Spades Nine of Hearts Nine of Diamonds Five of Clubs Six of Diamonds Eight of Hearts Five of Spades Four of Hearts Deuce of Clubs Three of Spades Four of Spades Six of Clubs Queen of Hearts King of Diamonds Five of Hearts Ten of Hearts Ten of Clubs Seven of Clubs Five of Diamonds Queen of Clubs Ace of Diamonds Seven of Spades Ace of Clubs Jack of Hearts Three of Hearts Ace of Hearts Three of Diamonds Jack of Spades Ten of Spades Six of Spades Queen of Diamonds Nine of Clubs

this is what I moved from CardDeck.h at the bottom of the class to the top of CardDeck.cpp
CPP / C++ / C Code:
const int CardDeck::numCards = 52;
const int CardDeck::numSuits = 4;

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

Thanks man for your help. I learned something and I owe congrats to you.

Bruce
  #8  
Old 31-Mar-2005, 13:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by QED
The error you are seeing results from one of the most common mistaskes in programming: the silly typo/mixup. Everyone does this, it's just a matter of how often. The more careful and experienced you are, the fewer errors of this type you will have.

I don't necessarily agree that more experienced programmers make fewer typos. Speaking from personal experience, as I got more experienced I typed faster and, percentage-wise I made almost as many typographical errors as before. The important point is that experienced programmers look a the error messages and the lines to which the error messages refer (sometimes the lines just before those in the messages). If the compiler says that there is a discrepency between line 26 and line 36: Look at line 26! Look at line 36!

#1 debugging tool: your little gray cells (sometimes stimulated by impulses from your optical nerves).


Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

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 22:46
I need help implementing kjc_13 C++ Forum 0 14-Feb-2005 16:00
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Error: (function) undeclared -first use of this function crystalattice C++ Forum 6 01-Nov-2004 04:36

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

All times are GMT -6. The time now is 17:50.


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