Another Stupid Newbie Question

E

Ed Dana

Maybe my brain's just tired, or maybe the book I'm reading from is just
crap, but... how do I do this Java reference in C++?

int tmpVal = this.getValue( );

Thanks in advance.

Ed.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Maybe my brain's just tired, or maybe the book I'm reading from is just
crap, but... how do I do this Java reference in C++?

int tmpVal = this.getValue( );

I suppose you meant the this.getValue()-part, which in C++ would be
this->getValue() since this is a pointer and not a reference.
 
A

Ambar Shukla

Maybe my brain's just tired, or maybe the book I'm reading from is just
crap, but... how do I do this Java reference in C++?

int tmpVal = this.getValue( );

Thanks in advance.

Ed.

"this" is a pointer, so you need to use the "->" operator instead of
"."
Try int tmpVal = this->getValue( );

Else directly:
int tmpVal = getValue();

Not quite sure why you are using "this"...


Cheers,
Ambar Shukla.
 
E

Ed Dana

Ambar said:
"this" is a pointer, so you need to use the "->" operator instead of
"."
Try int tmpVal = this->getValue( );

Else directly:
int tmpVal = getValue();

Not quite sure why you are using "this"...

I'm using it because "int tmpVal = getValue();" got me an error: "29
Hand.cpp `getValue' undeclared (first use this function)" despite the
fact that it is declared public in my header file.

I also tried the -> pointer as well and that got me the error: "29
W:\Projects\C++\Game\BlackJack\src\Hand.cpp invalid use of `this' in
non-member function," and, yet, the function calling it is also declared
in the header section.

Do I need to declare it something else? Like "virtual?" I'm still
confused on this one.

Ed.
 
R

Rolf Magnus

Ed said:
I'm using it because "int tmpVal = getValue();" got me an error: "29
Hand.cpp `getValue' undeclared (first use this function)" despite the
fact that it is declared public in my header file.

Post a minimal, but complete program that shows the problem.
I also tried the -> pointer as well and that got me the error: "29
W:\Projects\C++\Game\BlackJack\src\Hand.cpp invalid use of `this' in
non-member function," and, yet, the function calling it is also declared
in the header section.

Well, that message means that you are defining a non-member function. Again,
you should post some actual code.
Do I need to declare it something else?

Dpends on how you do it currently.
Like "virtual?"

virtual is only needed for polymorphism.
 
D

Default User

Ed said:
I'm using it because "int tmpVal = getValue();" got me an error: "29
Hand.cpp `getValue' undeclared (first use this function)" despite the
fact that it is declared public in my header file.

I also tried the -> pointer as well and that got me the error: "29
W:\Projects\C++\Game\BlackJack\src\Hand.cpp invalid use of `this' in
non-member function," and, yet, the function calling it is also
declared in the header section.

Do I need to declare it something else? Like "virtual?" I'm still
confused on this one.

These sorts of guessing games are irritating and pointless. Post a
complete, minimal program that demonstrates the problem.

See the newsgroup FAQ for more posting tips.





Brian
 
E

Ed Dana

Rolf said:
Dpends on how you do it currently.

All right, the header file looks like this:
======================================================================
#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);

private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCard[10];
};
======================================================================

The body looks like this:
======================================================================
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

// Declarations...
#ifndef _Hand_H_
#include "Hand.h"
#endif

// Definitions...
bool clsSoft = false;
int clsCount = 0;
int clsHidden = 0;
Card clsCard[10];

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

}

// Methods...
bool add(Card prmCard) {
clsCard[clsCount++] = prmCard;
};

bool canDouble() {
bool tmpBool = false;
int tmpVal = this->getValue( ); <-- This line fails
if ( clsCount == 2
&& (tmpVal == 10 || tmpVal == 11)
)
tmpBool = true;
return tmpBool;
};

bool canSplit() {
;
};

int getSize() {
return clsCount;
};

int getValue() {
int sumInt = 0;
bool thereBeAcesHere = false;
for ( int h = 0; h < clsCount; h++) {
int tmpInt = clsCard[h].getFaceValue();
sumInt += tmpInt;
if (tmpInt==1) thereBeAcesHere = true;
}
clsSoft = false;
if (thereBeAcesHere && sumInt < 12) {
sumInt += 10;
clsSoft = true;
}
return sumInt;
};

bool isBlackjack() {
;
};

bool isBusted() {
;
};

bool isSoft() {
;
};

void setHidden(int prmHideNo) {
clsHidden = prmHideNo;
};
======================================================================

The error I get is "invalid use of `this' in non-member function,"
which, as far as I know, is a member function. But I'm sure that I'm
missing something obvious here.
virtual is only needed for polymorphism.

That's what I thought too. :) But then, that's why I'm asking questions,
cause I'm not sure what the issue is.

Ed.
 
E

Ed Dana

Default said:
These sorts of guessing games are irritating and pointless. Post a
complete, minimal program that demonstrates the problem.

See the newsgroup FAQ for more posting tips.

Dude. Take a chill pill. I know the posting rules.

My last post I was criticized for not posting enough code. If I posted
the entire source, I'd be criticized for posting too much code. So, the
question becomes "how much code is enough?" For this question, I was
merely trying to find out what the proper method was, and that's using
the "->" pointer. Now, I know the answer and it's obvious the problem is
bigger than which pointer to use, so I'll post more info, but you still
need to chill.

Why is it programmers are some of the most up-tight people on Earth,
I'll never know.

Ed.
 
R

Rolf Magnus

Ed said:
All right, the header file looks like this:
======================================================================
#define _Hand_H_

You shouldn't use this identifier. It's reserved.

[...]
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

// Declarations...
#ifndef _Hand_H_
#include "Hand.h"
#endif

Put the #ifndef into the header. Otherwise it will be pretty much useless.
// Definitions...
bool clsSoft = false;
int clsCount = 0;
int clsHidden = 0;
Card clsCard[10];

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

}

// Methods...
bool add(Card prmCard) {
clsCard[clsCount++] = prmCard;
};

This is a non-member function. It has nothing to do with the add() member
function of your class. Just like you did with the constructor, you have to
qalify the name, i.e. write:

bool Hand::add(Card prmCard)

Since you have out-of-class duplicates of all the member variables, you
don't get an error from the access. However, as it's now, the function does
not access the member variable clsCard of class Hand, but the global
variable.
bool canDouble() {
bool tmpBool = false;
int tmpVal = this->getValue( ); <-- This line fails

Well, since the function is not defined as member variable, there is
no 'this' pointer.

[...]
The error I get is "invalid use of `this' in non-member function,"
which, as far as I know, is a member function. But I'm sure that I'm
missing something obvious here.

To be honest, this all looks as if you are trying to get your program
working by guessing how C++ works, which won't work too well (I have been
down that road myself). Get a good C++ book and read it.
 
V

Victor Bazarov

Ed said:
All right, the header file looks like this:

Just a few comments so that next time your code looks better...
======================================================================
#define _Hand_H_

Identifiers that begin with an underscore and a capital letter are
reserved by the implemenation. You cannot use them.
#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);

private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCard[10];
};
======================================================================

The body looks like this:
======================================================================
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

// Declarations...
#ifndef _Hand_H_

You have the include guard in the header file, you don't need one
here (regardless of what some book authors say). It doesn't hurt
anything except readablity of your code.
#include "Hand.h"
#endif

// Definitions...
bool clsSoft = false;
int clsCount = 0;
int clsHidden = 0;
Card clsCard[10];

What are those supposed to be? Why do you think you need those
objects at the namespace scope? They look strikingly similar to
the member data in 'Hand' class.
// Constructors...
Hand::Hand( ) {

Did you mean to initialise your member data here somehow?
}

// Methods...
bool add(Card prmCard) {

Are you trying to define a member function here? You should
consider naming it correctly:

bool Hand::add(Card prmCard) {

Otherwise, you're defining a non-member function.
clsCard[clsCount++] = prmCard;

This function declares a return value type, but no 'return'
statement seems to be in sight. Did you mean to make it 'void'?
If this function is ever called and its return value used, you
have undefined behaviour (UB).

The trailing semicolon after a function body in the file scope
is a syntax error. Please remove all of them.
bool canDouble() {

Same here. You probably wanted to write

bool Hand::canDouble() {
bool tmpBool = false;
int tmpVal = this->getValue( ); <-- This line fails
if ( clsCount == 2
&& (tmpVal == 10 || tmpVal == 11)
)
tmpBool = true;
return tmpBool;
};

bool canSplit() {
;

Again, a function returning 'bool' without any 'return'
statement. A disaster waiting to happen.
};

int getSize() {
return clsCount;
};

int getValue() {
int sumInt = 0;
bool thereBeAcesHere = false;
for ( int h = 0; h < clsCount; h++) {
int tmpInt = clsCard[h].getFaceValue();
sumInt += tmpInt;
if (tmpInt==1) thereBeAcesHere = true;
}
clsSoft = false;
if (thereBeAcesHere && sumInt < 12) {
sumInt += 10;
clsSoft = true;
}
return sumInt;
};

bool isBlackjack() {
;
};

bool isBusted() {
;
};

bool isSoft() {
;
};

void setHidden(int prmHideNo) {
clsHidden = prmHideNo;
};
======================================================================

The error I get is "invalid use of `this' in non-member function,"
which, as far as I know, is a member function. But I'm sure that I'm
missing something obvious here.

You are. See above.

V
 
E

Ed Dana

Rolf said:
This is a non-member function. It has nothing to do with the add() member
function of your class. Just like you did with the constructor, you have to
qalify the name, i.e. write:

bool Hand::add(Card prmCard)

Ahhh... I forgot about this little fact. My other classes do this but it
didn't even register in my mind when I looked at them. I knew it was
another newbie mistake. :) I was assuming it was defined in the header
file and I was missing something there.
Since you have out-of-class duplicates of all the member variables, you
don't get an error from the access. However, as it's now, the function does
not access the member variable clsCard of class Hand, but the global
variable.




Well, since the function is not defined as member variable, there is
no 'this' pointer.

Fair enough. I'm used to higher level languages, and I mean higher level
than even Java, which I find somewhat crude. Since C++ is lower level, I
tend to forget about some of its idioms.
To be honest, this all looks as if you are trying to get your program
working by guessing how C++ works, which won't work too well (I have been
down that road myself). Get a good C++ book and read it.

I do have a book. I have been reading it. But I'm beginning to suspect
it's a lousy book. That, and the fact that I'm not doing this full time.
A little bit here, a little bit there. That's how I can forget about
something like qualifying the member function with the class name in
order to make it a member class.

Thanks for the assist. I appreciate it.

Ed.
 
I

Ian Collins

Ed said:
Dude. Take a chill pill. I know the posting rules.
The follow them, for your benefit.
My last post I was criticized for not posting enough code. If I posted
the entire source, I'd be criticized for posting too much code.
There are two good reasons for posting a complete, minimal program that
demonstrates the problem:

1 - The process of distilling your problem down to a small example often
flushes the bug for you.

2 - It enables someone interesting helping to compile to code and find
the problem.
 
J

JLS

All right, the header file looks like this:Just a few comments so that next time your code looks better...
======================================================================
#define _Hand_H_Identifiers that begin with an underscore and a capital letter are
reserved by the implemenation. You cannot use them.






#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);
private:
bool clsSoft;
int clsCount;
int clsHidden;
Card clsCard[10];
};
======================================================================
The body looks like this:
======================================================================
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
// Declarations...
#ifndef _Hand_H_You have the include guard in the header file, you don't need one
here (regardless of what some book authors say). It doesn't hurt
anything except readablity of your code.
#include "Hand.h"
#endif
// Definitions...
bool clsSoft = false;
int clsCount = 0;
int clsHidden = 0;
Card clsCard[10];What are those supposed to be? Why do you think you need those
objects at the namespace scope? They look strikingly similar to
the member data in 'Hand' class.


// Constructors...
Hand::Hand( ) {Did you mean to initialise your member data here somehow?



// Methods...
bool add(Card prmCard) {Are you trying to define a member function here? You should
consider naming it correctly:

bool Hand::add(Card prmCard) {

Otherwise, you're defining a non-member function.
clsCard[clsCount++] = prmCard;This function declares a return value type, but no 'return'
statement seems to be in sight. Did you mean to make it 'void'?
If this function is ever called and its return value used, you
have undefined behaviour (UB).
};The trailing semicolon after a function body in the file scope
is a syntax error. Please remove all of them.


bool canDouble() {Same here. You probably wanted to write

bool Hand::canDouble() {
bool tmpBool = false;
int tmpVal = this->getValue( ); <-- This line fails
if ( clsCount == 2
&& (tmpVal == 10 || tmpVal == 11)
)
tmpBool = true;
return tmpBool;
};
bool canSplit() {
;Again, a function returning 'bool' without any 'return'
statement. A disaster waiting to happen.




int getSize() {
return clsCount;
};
int getValue() {
int sumInt = 0;
bool thereBeAcesHere = false;
for ( int h = 0; h < clsCount; h++) {
int tmpInt = clsCard[h].getFaceValue();
sumInt += tmpInt;
if (tmpInt==1) thereBeAcesHere = true;
}
clsSoft = false;
if (thereBeAcesHere && sumInt < 12) {
sumInt += 10;
clsSoft = true;
}
return sumInt;
};
bool isBlackjack() {
;
};
bool isBusted() {
;
};
bool isSoft() {
;
};
void setHidden(int prmHideNo) {
clsHidden = prmHideNo;
};
======================================================================
The error I get is "invalid use of `this' in non-member function,"
which, as far as I know, is a member function. But I'm sure that I'm
missing something obvious here.You are. See above.

Of course, it would also be nice to see the keyword "const" used,
especially on the accessor methods.
 
E

Ed Dana

Ian said:
There are two good reasons for posting a complete, minimal program that
demonstrates the problem:

1 - The process of distilling your problem down to a small example often
flushes the bug for you.

2 - It enables someone interesting helping to compile to code and find
the problem.

I understand that. And in previous posts have even done so. But this
post started as a simple question and evolved from there.

Ed.
 
D

Default User

Ed said:
Dude. Take a chill pill. I know the posting rules.

My last post I was criticized for not posting enough code.

Correctly so.
If I
posted the entire source, I'd be criticized for posting too much
code.

Did I tell you to do that? Reread.
So, the question becomes "how much code is enough?"

A COMPLETE MINIMAL PROGRAM THAT DEMONSTRATES THE PROBLEM. Think you can
get it now?
Why is it programmers are some of the most up-tight people on Earth,
I'll never know.

Idiot.




Brian
 
D

Default User

Ed said:
You're very gracious.

And you are rude and uncooperative when you come here asking for free
advice. You ought to be ashamed of your behavior, and you owe an
apology all these kind people who helped you in spite of your lack of
deceny.




Brian
 
E

Ed Dana

Default said:
And you are rude and uncooperative when you come here asking for free
advice. You ought to be ashamed of your behavior, and you owe an
apology all these kind people who helped you in spite of your lack of
deceny.

Yes, yes, yes... I should have known that my question could only have
been answered by posting source code first when it was a simple question
about what method I should have been using.

I would have posted the code eventually, and was preparing to do so when
it became obvious that the problem was broader than my simple question.
Do forgive me for exercising my discretion.

[rolls eyes]

Ed.
 
D

Default User

Ed said:
Yes, yes, yes... I should have known that my question could only have
been answered by posting source code first when it was a simple
question about what method I should have been using.

I would have posted the code eventually, and was preparing to do so
when it became obvious that the problem was broader than my simple
question. Do forgive me for exercising my discretion.

The apology I was suggesting wasn't for not posting the code, although
that would have been nice. It was for needlessly insulting the people
who helped you.

Had you wanted to insult me personally, you should have done so,
without doing so to everyone else including those that have done quite
a bit of work on your behalf.

As I said, you should be ashamed. You don't seem to be.




Brian
 
O

Old Wolf

My last post I was criticized for not posting enough code. If I posted
the entire source, I'd be criticized for posting too much code. So, the
question becomes "how much code is enough?" For this question, I was
merely trying to find out what the proper method was, and that's using
the "->" pointer. Now, I know the answer and it's obvious the problem is
bigger than which pointer to use, so I'll post more info, but you still
need to chill.

Why is it programmers are some of the most up-tight people on Earth,
I'll never know.

Don't worry, plenty of us aren't. In fact I wouldn't mind if there
were a compiler warning for forgetting to include Class:: on the
definition of member functions; I do it all the time and then
puzzle over why the wrong function overload is being called, etc.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top