Help me, Obi C Kenobi...

E

Ed Dana

You're my only hope!

OK, I gots a mystery on my hands. I'm hoping someone can tell me just
what the **** is going on here.

I'm familiar with other object oriented programming tools, such as Java
& REBOL. In an effort to become more familiar with C++, I decided to
write an object oriented BlackJack program. After writing some static
routines for Faces and Suits, I turned my attention to creating Cards
and Hands. Cards were no problem, Hands are starting to tick me off. :)

First, my header file for Hands:
======================================================================
#ifndef _Hand_H_
#define _Hand_H_

#include "Card.h"

class Hand {
public:
Hand();

bool add(Card prmCard);
bool canDouble();
bool canSplit();
int getSize();
int getValue();
bool isBlackjack();
bool isBusted();
bool isSoft();
void setHidden(int prmHideNo);
string toString();
string toString(bool prmLong);

private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCards[0];
}
#endif //_Hand_H_
======================================================================

Next, my C++ file for Hands:
======================================================================
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

// Declarations...
#include "Hand.h"

// Definitions...
int clsCount;
int clsHidden;
bool clsSoft;
Card clsCards[0];

// Constructors...
Hand::Hand( ) {

}

// Methods...
.... All methods are currently stubs.
======================================================================

Please notice the "definitions" section. When compiled, it produces the
following error: "extraneous `int' ignored."

When I remove all definitions in that section, I get:
----------------------------------------------------------------------
12 Hand.cpp new types may not be defined in a return type
12 Hand.cpp return type specification for constructor invalid
----------------------------------------------------------------------

But when I change the definition section to remove the first, and only
first, int declaration, so that it looks like this:
======================================================================
// Definitions...
clsCount;
int clsHidden;
bool clsSoft;
Card clsCards[0];
======================================================================

It compiles successfully!

AND! More entertainingly! If I switch the declarations around so that
the bool is first, like this:
======================================================================
// Definitions...
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCards[0];
======================================================================

I get the error: "'bool' is now a keyword." But, once again, like int,
leaving the bool keyword out compiles successfully.

What's going on?

I'm a C++ newbie, and I'm probably making some kinda C++ newbie mistake,
but because I a newbie, I've got no idea what that mistake might be.

I'm using DevCpp to compile this. Clues welcome.

Ed.
 
I

Ian Collins

Ed said:
You're my only hope!

OK, I gots a mystery on my hands. I'm hoping someone can tell me just
what the **** is going on here.
Posting incomplete code doesn't help your cause, you should distil the
problem down to something that can compile and exhibits your problem.
But...
// Definitions...
int clsCount;
int clsHidden;
bool clsSoft;
Card clsCards[0];
In what context are these? They appear to duplicate your class members.
 
G

gw7rib

[For some reason the google reply command isn't quoting correctly, so
I'm having to edit in exactly the spot I am trying to highlight, but
hopefully what I quote will be the same as what you actually
posted...]

You're my only hope!

OK, I gots a mystery on my hands. I'm hoping someone can tell me just
what the **** is going on here.

I'm familiar with other object oriented programming tools, such as Java
& REBOL. In an effort to become more familiar with C++, I decided to
write an object oriented BlackJack program. After writing some static
routines for Faces and Suits, I turned my attention to creating Cards
and Hands. Cards were no problem, Hands are starting to tick me off. :)

First, my header file for Hands:
======================================================================
#ifndef _Hand_H_
#define _Hand_H_

#include "Card.h"

class Hand {
public:
Hand();

bool add(Card prmCard);
bool canDouble();
bool canSplit();
int getSize();
int getValue();
bool isBlackjack();
bool isBusted();
bool isSoft();
void setHidden(int prmHideNo);
string toString();
string toString(bool prmLong);

private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCards[0];
}

Here's your real problem. You need a semi-colon here, and you haven't
put one in.
#endif //_Hand_H_
======================================================================

Next, my C++ file for Hands:
======================================================================
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

// Declarations...
#include "Hand.h"

So when the compiler gets here, it is expecting you to give the name
the the "Hand" that it thinks you are declaring. Hence the funny
results.
// Definitions...
int clsCount;
int clsHidden;
bool clsSoft;
Card clsCards[0];
 
T

Thomas J. Gritzan

Ed said:
You're my only hope!

OK, I gots a mystery on my hands. I'm hoping someone can tell me just
what the **** is going on here. [...]
First, my header file for Hands:
======================================================================
#ifndef _Hand_H_
#define _Hand_H_

#include "Card.h"

class Hand { [...]
Card clsCards[0];

An array can't be zero sized. Do you want a dynamic array? Then use
std::vector.
}
Semicolon?

#endif //_Hand_H_
======================================================================

Next, my C++ file for Hands:
====================================================================== [...]
// Declarations...
#include "Hand.h"

// Definitions...
int clsCount;
int clsHidden;
bool clsSoft;
Card clsCards[0];

What are these for?

You don't have to define class member variables unless they are static.
Then, you have to prefix them with the class name like so:

int Hand::clsCount;

But they aren't static...

[...description of some errors...]
What's going on?

Its the missing semicolon after the class Hand definition.
 
S

Scott McPhillips [MVP]

Ed said:
I'm a C++ newbie, and I'm probably making some kinda C++ newbie mistake,
but because I a newbie, I've got no idea what that mistake might be.

The mistake is that the declaration of a class must be terminated with a
semicolon. You left it out.

class Hand {
....
};
 
I

I V

OK, I gots a mystery on my hands. I'm hoping someone can tell me just
what the **** is going on here.
class Hand { [...]
}
^ Note the lack of a semicolon here. That might cause mysterious
problems immediately after where Hand.h is included.
 
E

Ed Dana

======================================================================
#ifndef _Hand_H_
#define _Hand_H_

#include "Card.h"

class Hand {
public:
Hand();

bool add(Card prmCard);
bool canDouble();
bool canSplit();
int getSize();
int getValue();
bool isBlackjack();
bool isBusted();
bool isSoft();
void setHidden(int prmHideNo);
string toString();
string toString(bool prmLong);

private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCards[0];
} <-- Semicolon here


Here's your real problem. You need a semi-colon here, and you haven't
put one in.

Blast! I knew it was going to be something annoyingly simple like that.
:) Thanks for the help, that definitely fixed the problem.

Ed.
 
R

red floyd

Ed said:
[redacted]
======================================================================
#ifndef _Hand_H_
#define _Hand_H_

Others have pointed out the error of the missing semicolon, so I'll go
with one of my pet peeves.

Your include guard is invalid. The Standard states that any identifier
with a leading underscore followed by an uppercase letter is reserved to
the implementation.

That means your compiler vendor (and the Standard Library vendor, if
they're not the same entity) can use an identifier such as _Hand_H_, but
*YOU* may not.

Just use HAND_H_ as your include guard.
 
J

John Ratliff

I wrote one for an intro C++ class a few years back. It's probably
horrible (I wasn't much of a coder back then), but if it gives you some
ideas. http://resume.technoplaza.net/c/blackjack.php

The source is included, though it will only compile with MSVC 6. (No,
not MSVC .NET -- it uses MFC stuff that is outdated).

--John Ratliff
 
O

Old Wolf

one of my pet peeves.

Your include guard is invalid. The Standard states that any identifier
with a leading underscore followed by an uppercase letter is reserved to
the implementation.

That means your compiler vendor (and the Standard Library vendor, if
they're not the same entity) can use an identifier such as _Hand_H_, but
*YOU* may not.

Just use HAND_H_ as your include guard.

Identifiers in uppercase starting with E are reserved for
future use by errno.h. So this technique will not work
for files starting with 'e'.
 
R

red floyd

Old said:
Identifiers in uppercase starting with E are reserved for
future use by errno.h. So this technique will not work
for files starting with 'e'.
Good point. I don't have ISO/IEC 9899 around, so I forgot about that
part, which was included in 14882 by incorporation.

Maybe if for a a generic include guard:

#ifndef IG_FILENAME_H_ /* IG stands for "Include Guard" */
#define IG_FILENAME_H_

// source

#endif
 
E

Ed Dana

red said:
Good point. I don't have ISO/IEC 9899 around, so I forgot about that
part, which was included in 14882 by incorporation.

Maybe if for a a generic include guard:

#ifndef IG_FILENAME_H_ /* IG stands for "Include Guard" */
#define IG_FILENAME_H_

// source

#endif

How about just DEF_FILENAME_H?

Just a thought.

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top