Overclock.net banner
1 - 12 of 12 Posts

·
Registered
Joined
·
34 Posts
Discussion Starter · #1 ·
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:

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;
}
 

·
Premium Member
Joined
·
1,592 Posts
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.
 

·
Registered
Joined
·
34 Posts
Discussion Starter · #4 ·
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!?"
 

·
Registered
Joined
·
821 Posts
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.
 

·
Registered
Joined
·
2,017 Posts
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.
 

·
Registered
Joined
·
821 Posts
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.
 

·
Premium Member
Joined
·
1,592 Posts
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.
 

·
Registered
Joined
·
821 Posts
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?
 

·
Premium Member
Joined
·
1,592 Posts
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.
 

·
Registered
Joined
·
34 Posts
Discussion Starter · #11 ·
Ah! Working now


Only one extra line in my class definition like you said instead of trying to create the object in a class method.

Dice d1;
 

·
Premium Member
Joined
·
1,592 Posts
Quote:

Originally Posted by Desmolas View Post
Ah! Working now


Only one extra line in my class definition like you said instead of trying to create the object in a class method.

Dice d1;
Good to hear! Good luck on the upcoming assignments!
 
1 - 12 of 12 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top