Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Error i can't fix!

Author
Message
MHS
15
Years of Service
User Offline
Joined: 16th Aug 2009
Location:
Posted: 8th Sep 2009 06:28
hi everyone
i have book "C++ Programming for Games Module I" from Game Institute and the code put

here for download is based on the codes in the book. but when i try to debug it, it

make these errors:
//////////////////////////////


////////////////////////////////
which i can't fix. i tried "if" instead of "switch" and it seemed to work out but "if"

makes garbage values. it also happens when i put "case"s of "switch" in {}s.
can anybody help me what's wrong?
i'm stuck just in the begginnig of my code and i can't go any furthur
thanks everybody

Attachments

Login to view attachments
Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 8th Sep 2009 12:11
Thats a basic coding structure problem, I think you will have to spend some more time learning the basics of C++ and how to structure your code and use the various conditionals. There are lots of great sites on the Internet that you can find C++ Tutorials on, just search google for "C++ Tutorial Beginners".

Once you understand how to structure the code, you will see the error, there would be no point in solving the problem for you as you more than likely wouldnt understand how it was done. Using books and code to learn from is a great idea, it is important though to make sure you understand the code Properly, before you try to use it.

If it ain't broke.... DONT FIX IT !!!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 8th Sep 2009 16:26
There are several things wrong with that code.
- Usually, classes should not try to create instances of themselves from within the class itself. The code you have in the "CreatClass" function would look much better in the main program than here.
- You try to create differently named variables within different case branches, which is what eventually triggers the error message. The compiler is telling you that different conditions would result in the creation of different variables, which is not allowed.
- Even if it were possible to create new instances like this, "knight", "thief" and "clerk" are local variables to the function, so how did you plan to further use them in the program? They are not visible from anywhere else outside "CreatClass".
- You don't really need to create differently named variables just to indicate what character the player is playing, if the only difference between them is the name and the value of some attributes. If there are lots of differences between the types, then I could imagine an inheritance structure, otherwise, a proper constructor taking a type parameter would be good.

I suggest you to read more about the creation and usage of classes.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Sep 2009 19:20
Quote: "Usually, classes should not try to create instances of themselves from within the class itself."


That's one of the things that I found really strange in Java, not that I ever studied it in depth. Now it seems that a number of frameworks are starting to use the technique. It always felt recursive to me.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 8th Sep 2009 21:27
Just put curly braces around the code between 'case' and 'break' (or the next 'case') statements. That way the new variables will be in a new scope, and so will always be initialised within that scope.

jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 8th Sep 2009 22:48 Edited at: 8th Sep 2009 22:48
What I do (and Diggsey said):


Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th Sep 2009 23:15
You're declaring the integers inside the braces. Once your code exits the braces your integer is no longer in scope and thus unavailable. If iInteger and iDifferentInteger are declared elsewhere and those are the variables you're trying to set, get rid of the "int" where you're making the assignment. It's declaring a local integer with the same name of one elsewhere.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 8th Sep 2009 23:47 Edited at: 8th Sep 2009 23:51
For MHS: maybe I can give a bit more help. That "CreatClass" function of yours could be transformed into a proper constructor with only a few changes. Example:



Then in the main() program you can put the lines which ask for the type of the player and create the instance:



(Sorry, I edited this post a few times, it's too late at night to write code... I hope it's OK now.)
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 9th Sep 2009 10:34
Just another example which occurred to me afterwards: you can leave the k selection in the constructor and then it needs even less changes to compile. I also forgot to mention that you should include "break" statements into the switch:



But the object still needs to be created in the main() program, outside the class, either with the pointer method shown in the previous post, or like this:

Login to post a reply

Server time is: 2024-10-01 12:25:12
Your offset time is: 2024-10-01 12:25:12