|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Need Help with C++ - Using Classes
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
PC Gamer
|
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;
}
Last edited by Desmolas : 03-06-09 at 08:24 AM |
||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Programmer
![]() |
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.
__________________
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
PC Gamer
|
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!?"
|
||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Programmer
![]() |
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 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.
|
|||||||||||||
|
|
|
|
|
#7 (permalink) | |
|
Case Modder
![]() |
Quote:
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Programmer
![]() |
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.
__________________
|
|||||||||||||
|
|
|
|
|
#9 (permalink) | |
|
Case Modder
![]() |
Quote:
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
|
#10 (permalink) | |||||||||||||
|
Programmer
![]() |
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.
__________________
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|