Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Another Stupid Newbie Question

Reply
Thread Tools

Another Stupid Newbie Question

 
 
Ed Dana
Guest
Posts: n/a
 
      01-29-2007
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.
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-29-2007
On Jan 29, 7:23 am, Ed Dana <(E-Mail Removed)> wrote:
> 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.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
Ambar Shukla
Guest
Posts: n/a
 
      01-29-2007


On Jan 29, 6:23 am, Ed Dana <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Ed Dana
Guest
Posts: n/a
 
      01-29-2007
Ambar Shukla wrote:
>
> On Jan 29, 6:23 am, Ed Dana <(E-Mail Removed)> wrote:
>
>>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"...


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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-29-2007
Ed Dana wrote:

> Ambar Shukla wrote:
>>
>> On Jan 29, 6:23 am, Ed Dana <(E-Mail Removed)> wrote:
>>
>>>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"...

>
> 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.

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-29-2007
Ed Dana wrote:

> Ambar Shukla wrote:
> >
> >On Jan 29, 6:23 am, Ed Dana <(E-Mail Removed)> wrote:
> >
> > > 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"...

>
> 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
 
Reply With Quote
 
Ed Dana
Guest
Posts: n/a
 
      01-29-2007
Rolf Magnus wrote:
> Ed Dana wrote:
>
>>Do I need to declare it something else?

>
>
> 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.

>>Like "virtual?"

>
> 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.
 
Reply With Quote
 
Ed Dana
Guest
Posts: n/a
 
      01-29-2007
Default User wrote:
> 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-29-2007
Ed Dana wrote:

> Rolf Magnus wrote:
>> Ed Dana wrote:
>>
>>>Do I need to declare it something else?

>>
>>
>> Dpends on how you do it currently.
>>

>
> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-29-2007
Ed Dana wrote:
> Rolf Magnus wrote:
>> Ed Dana wrote:
>>
>>> Do I need to declare it something else?

>>
>>
>> Dpends on how you do it currently.
>>

>
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stupid, STUPID question! rincewind HTML 25 05-08-2009 01:07 PM
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Another stupid newbie question Byte Python 10 02-19-2006 07:58 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM



Advertisments