Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-06-09   #1 (permalink)
PC Gamer
 
Desmolas's Avatar
 
intel ati

Join Date: Nov 2008
Posts: 11

Rep: 0 Desmolas Unknown
Unique Rep: 0
Trader Rating: 0
Default Need Help with C++ - Using Classes

Yo, im just learning C++ and the class has moved onto classes. Im having a problem with the scope of an object.

The problem appears to be that im trying to access an object that was created in a seperate method. Thats as far as i see it. I dont know how i would reference the object from another method.

Purpose: The program generates random numbers of a dice (1 - 6) if the random value of the dice roll != 6 then player loses 1 point. If the roll 5 times and the score is decremented to 0 then they lose. The aim is to roll a 6 to win as quick as possible. The player has an initial score of 5 or 5 'lives'

Ahem, you can critisize and point out other amateurish mistakes only after youve helped Ive been doing this only 3 weeks

Code:
void Game::startGame()
{

	cout << "Start dice game...n";

	for(int i = 5; playerLose = 1; i++)    //playerLose is a boolean check
	{
		
		Dice d1;      // creating Dice object. I dont even know if your allowed to create 
an object for a class in another class's method. Seems like it might be bad practice or something.
		Game::turn();	//makes the game roll the dice

		if (d1.getValue() != 6)
		{
			decrementScore();
		}
		else
		{
			Win();
			break;
		}
		
		if (getScore() == 0)
		{
			Lose();
			break;
		}

	}
	return;
}

void Game::turn()
{
	d1.roll();    // <---- my compiler is telling me "error C2065: 'd1' : 
undeclared identifier" and "error C2228: left of '.roll' must have 
class/struct/union" Im basically seeing that as the compiler saying "This aint 
no object i know about!" Even though i created it in the startGame() method. 
I just need to tell it what object im talking about.
		cout << d1.getValue() << "  " << getScore() << endl; // 
same thing with d1.getValue in this line.
	return;
}
__________________
System: Neil Freakin' Peart
CPU
i7 920 2.66Ghz
Motherboard
Asus P6T Intel X56
Memory
6GB OCZ DDR3 1600MHz
Graphics Card
HD 4870 1GB
Hard Drive
1TB Hitachi Deskstar
Power Supply
Corsair TX 850w
Case
Coolermaster RC-1000
CPU cooling
Coolermaster Hyper Z600
GPU cooling
<stock>
OS
Vista 64bit
Monitor
Samsung 215tw 21"

Last edited by Desmolas : 03-06-09 at 08:24 AM
Desmolas is offline   Reply With Quote
Old 03-06-09   #2 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

Scope is your problem.

If you want to use the Dice class within the Game class, you need to have a member of the Game class that is a Dice object, so when you create Dice dl, it is a member of the class. Have a provate member of type Dice and use a method to initialize it.

I'm kind of out of it so if that doesn't make sense I'll try to clarify.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD
xtascox is offline   Reply With Quote
Old 03-06-09   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Code:
class Dice 
{
    // ...
};

class Game
{
    Dice dice;

    // ...
};
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 03-07-09   #4 (permalink)
PC Gamer
 
Desmolas's Avatar
 
intel ati

Join Date: Nov 2008
Posts: 11

Rep: 0 Desmolas Unknown
Unique Rep: 0
Trader Rating: 0
Default

Ooh, thanks for that xtascox. That helps me to see how it all works now.

And thanks for pointing it out what i should do Spotswood

This classes business is actually quite a revelation to me since ive been doing VisualBasic procedural stuff all my programming days. Im really getting into it...

Its like, "Why didnt they teach me this to begin with!?"
__________________
System: Neil Freakin' Peart
CPU
i7 920 2.66Ghz
Motherboard
Asus P6T Intel X56
Memory
6GB OCZ DDR3 1600MHz
Graphics Card
HD 4870 1GB
Hard Drive
1TB Hitachi Deskstar
Power Supply
Corsair TX 850w
Case
Coolermaster RC-1000
CPU cooling
Coolermaster Hyper Z600
GPU cooling
<stock>
OS
Vista 64bit
Monitor
Samsung 215tw 21"
Desmolas is offline   Reply With Quote
Old 03-07-09   #5 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by Desmolas View Post
"Why didnt they teach me this to begin with!?"
So you can discover just how much the other way sucks.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 03-07-09   #6 (permalink)
Programmer
 
intel nvidia

Join Date: Nov 2006
Posts: 1,977

Rep: 132 version2 is acknowledged by manyversion2 is acknowledged by many
Unique Rep: 111
Trader Rating: 9
Default

Classes are cool for medium-sized projects. They can help you stay really organized. However, one thing I've picked up is that with larger projects, class design deteriorates because of the increasing complexity of the program.

BTW, theseare a good references for using classes in c++:
http://www.cplusplus.com/doc/tutorial/classes.html
http://www.cplusplus.com/doc/tutorial/classes2.html
http://www.cplusplus.com/doc/tutorial/inheritance.html

Quote:
Originally Posted by Desmolas View Post
Its like, "Why didnt they teach me this to begin with!?"
There has actually been a lot of discussion about this. Some teachers argue that this is the best way to teach intro to comp sci classes. I actually don't think this approach is the best, because it basically skips the programming aspect, which is the most crucial part.
__________________
System: Duo
CPU
Core 2 Duo E6600 (B2)
Motherboard
Asus P5B Deluxe
Memory
G.SKILL HZ 2x1gb DDR2 800
Graphics Card
EVGA 9600GSO 384mb (G92)
Hard Drive
1TB + WD 640gb, Maxtor 160gb
Sound Card
Audigy 2 ZS
Power Supply
FSP 450W
Case
Ultra Aluminus
CPU cooling
Noctua U12P
GPU cooling
Stock
OS
Win 7 x64, Ubuntu
Monitor
20.1'' SOYO LCD
version2 is offline   Reply With Quote
Old 03-07-09   #7 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by version2 View Post
Classes are cool for medium-sized projects. They can help you stay really organized. However, one thing I've picked up is that with larger projects, class design deteriorates because of the increasing complexity of the program.
Following the S.O.L.I.D. OO design principles and using Test Driven Development will help.
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 03-08-09   #8 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

Quote:
Originally Posted by version2 View Post
Classes are cool for medium-sized projects. They can help you stay really organized. However, one thing I've picked up is that with larger projects, class design deteriorates because of the increasing complexity of the program.
The size of the program isn't really related to classes at all. In fact, the larger the program, the more functional classes become. They keep everything more organized and less cluttered. I wouldn't even consider taking the time to write a class for a small to medium sized project unless I knew that class would be useful to me at a later point. But anything larger and I focus on as much class use as possible.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD
xtascox is offline   Reply With Quote
Old 03-08-09   #9 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by xtascox View Post
...I wouldn't even consider taking the time to write a class for a small to medium sized project unless I knew that class would be useful to me at a later point...
Its okay to write crappy code for small to medium projects, but that's somehow unacceptable for larger ones?
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 03-08-09   #10 (permalink)
Programmer
 
xtascox's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: Dallas, PA
Posts: 778

Rep: 37 xtascox is acknowledged by some
Unique Rep: 36
Trader Rating: 1
Default

Quote:
Originally Posted by Spotswood View Post
Its okay to write crappy code for small to medium projects, but that's somehow unacceptable for larger ones?
How do you manage to get that out of what I said? All I said is that I tend to not use class based code for smaller projects.
__________________

System: SVT
CPU
Q6600 @ 3.2ghz 1600fsb
Motherboard
EVGA 780i FTW
Memory
4GB OCZ Platimum
Graphics Card
2X EVGA 8800GTS 512
Hard Drive
160GB Seagate Barracuda 7200 SATA
Sound Card
Onboard Realtek ALC883
Power Supply
CORSAIR 750TX
Case
Chenming 901A
CPU cooling
Arctic Freezer 7 + Arctic Silver 5
GPU cooling
Stock Heatsink and fan
OS
Windows Vista Ultimate 64
Monitor
Hanns-G 22" LCD
xtascox is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 11:18 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.16525 seconds with 8 queries