• Class Problems, calling from other scopes.
    26 replies, posted
  • I'm trying to do things with classes, but the tutorials don't seem to cover this. I have a header file that handles user input. In the user input, when space is pressed I create a new member of the class and increment the integer "ballamount" by one. I also call the ball[ballamount].ballinit(); function. So this creates a new ball and sets some variables. Now, in my main loop, I want to call the ballmove() and ballshow() functions in a for loop. [code]for(int i = 1; i <= balls; i++){ [highlight]ball[balls].ballmove();[/highlight] [highlight]ball[balls].ballshow();[/highlight] }[/code] It looks like that. The code in user input looks like this: [code]if(usrinput.key.keysym.sym == SDLK_SPACE) { //If Space is pressed balls++; //Add a ball ball ball[balls]; ball[balls].ballinit(); }[/code] This returns errors: [code]/home/craig/programming/particles/main.cpp|26|error: [b]expected unqualified-id before ‘[’ token|[/b][/code] for the highlighted lines in the main loop. So I have no idea how I should go about this. Thanks for any help.
  • Your code makes me want to strangle a kitten. It makes it horribly confusing when you have a variable named the EXACT same thing as a class.
  • No the variable is balls, the class is ball :). I remember because of an in joke you don't know, so it actually helps.
  • [cpp]ball ball[balls];[/cpp] Nope, the classname == variablename. [cpp]#include <vector> [...] typedef std::vector<ball> ballvec; [...] ballvec balls(INIT_SIZE); [...] if(usrinput.key.keysym.sym == SDLK_SPACE) { //If Space is pressed //Add a ball ball new_ball; new_ball.ballinit(); balls.push_back(new_ball); } [...] for(ballvec::iterator iter = balls.begin(); iter != balls.end(); ++iter){ iter->ballmove(); iter->ballshow(); }[/cpp] Will this do? Also, what's with all the balls lately?
  • [QUOTE=nos217;16005781]No the variable is balls, the class is ball :). I remember because of an in joke you don't know, so it actually helps.[/QUOTE] ball is a class and an array though....
  • Yikes I've never used Vectors. I appreciate the help but do they have to be used in this situation? [editline]12:01AM[/editline] [QUOTE=high6;16005859]ball is a class and an array though....[/QUOTE] Shit no that's making a new member of the class. I thought you could do that.
  • [QUOTE=PvtCupcakes;16005904]Capitalize your classes.[/QUOTE] don't tell him what to do.
  • [QUOTE=nos217;16005865]Yikes I've never used Vectors. I appreciate the help but do they have to be used in this situation? [editline]12:01AM[/editline] Shit no that's making a new member of the class. I thought you could do that.[/QUOTE] You can, but as high6 said it makes certain people want to strangle a kitten. Don't be scared about the vector, it's piss easy and fits here pretty well. It's just a dynamic array C++-style. You can easily find more info on the net if you wanna know more.
  • Ah right, in that case, I'm off to learn classes! Thanks a lot for the advice, I was going to have to learn it sometime. [editline]12:06AM[/editline] [QUOTE=PvtCupcakes;16005904]Capitalize your classes.[/QUOTE] Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?
  • [QUOTE=nos217;16005949]Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?[/QUOTE] I'd say it's a generally well accepted syntax.
  • [QUOTE=ZeekyHBomb;16005844]Also, what's with all the balls lately?[/QUOTE] Dude I fucking love balls.
  • [QUOTE=ZeekyHBomb;16005844] Also, what's with all the balls lately?[/QUOTE] Balls are fucking amazing.. Plus They are a easy to make shape :D
  • [QUOTE=nos217;16005949]Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?[/QUOTE] Find a piece of good code that doesn't capitalise class names.
  • [QUOTE=ZeekyHBomb;16013730]The C++ Standard Library.[/QUOTE] Good point, but I believe the STL classes can be seen more as base types, like int or float.
  • [QUOTE=ZeekyHBomb;16013913]I beg to differ. Anyways, how about Boost? Or zlib?[/QUOTE] Let me rephrase: Show me a good piece of code [i]that does not constitute a library or coding tool[/i] that doesn't capitalise it's classes.
  • Basing syntax on an "inside joke" probably isn't a good idea, contrary to common FUCKING SENSE.
  • [QUOTE=Deco Da Man;16013980]Let me rephrase: Show me a good piece of code [i]that does not constitute a library or coding tool[/i] that doesn't capitalise it's classes.[/QUOTE] Show me how it's relevant.
  • [QUOTE=Deco Da Man;16013980]Let me rephrase: Show me a good piece of code [i]that does not constitute a library or coding tool[/i] that doesn't capitalise it's classes.[/QUOTE] Stubborn, are we? cURL (not libcurl). I don't know much open-source programs; that means I know a bunch, but not their actual code. [editline]02:02PM[/editline] And I just looked at the Gimp source-code, it's only using lower-case characters, too.
  • [QUOTE=Deco Da Man;16013980]Let me rephrase: Show me a good piece of code [i]that does not constitute a library or coding tool[/i] that doesn't capitalise it's classes.[/QUOTE] Personally I think people should be allowed to code however the fuck they like. Except microsoft. THE MICROSOFT WINDOWS API LIKES TO HAVE EVERYTHING IN CAPITALS BECAUSE IT IS CLEARLY A GOOD IDEA FOR YOUR CODE TO LOOK LIKE THIS.
  • [QUOTE=Catdaemon;16016064]Personally I think people should be allowed to code however the fuck they like. Except microsoft. THE MICROSOFT WINDOWS API LIKES TO HAVE EVERYTHING IN CAPITALS BECAUSE IT IS CLEARLY A GOOD IDEA FOR YOUR CODE TO LOOK LIKE THIS.[/QUOTE] Hell, they use Systems Hungarian there.
  • [QUOTE=jefflol;16014030]Basing syntax on an "inside joke" probably isn't a good idea, contrary to common FUCKING SENSE.[/QUOTE] It's common sense to make things easier to remember things. That's what I'm doing. It's my coding style and it works fine for me. And you can't hold me having this problem against me because it has no relevance to my coding style.
  • [code] if(usrinput.key.keysym.sym == SDLK_SPACE) { //If Space is pressed balls++; //Add a ball ball ball[balls]; ball[balls].ballinit(); } [/code] Why don't you use numballs or num_balls or numberOfBalls for the integer, Ball/ball for the class name and ball[b]s[/b] for the array. And why don't you call ballinit init instead. You've got too many balls I'm afraid, and the compiler knows this.
  • [QUOTE=nos217;16022481]It's common sense to make things easier to remember things. That's what I'm doing. It's my coding style and it works fine for me. And you can't hold me having this problem against me because it has no relevance to my coding style.[/QUOTE] Good habbits are as hard to break as bad ones. If you start doing stupid shit like this, you'll end up getting used to it. When you're writing code for other people, do you think they'll know your inside jokes?
  • Hmm, thanks everyone :). I'll no longer use silly names haha. Anyway, I removed all of that now since I'm going to use vectors instead. Thanks :P.