1. Post #1
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    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++){
        ball[balls].ballmove();
        ball[balls].ballshow();
    }
    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();
    }
    This returns errors:

    Code:
    /home/craig/programming/particles/main.cpp|26|error: expected unqualified-id before [ token|
    for the highlighted lines in the main loop. So I have no idea how I should go about this. Thanks for any help.

  2. Post #2
    high6's Avatar
    September 2008
    197 Posts
    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.

  3. Post #3
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    No the variable is balls, the class is ball :). I remember because of an in joke you don't know, so it actually helps.

  4. Post #4
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    ball ball[balls];
    Nope, the classname == variablename.

    #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();
    }
    Will this do?

    Also, what's with all the balls lately?

  5. Post #5
    high6's Avatar
    September 2008
    197 Posts
    No the variable is balls, the class is ball :). I remember because of an in joke you don't know, so it actually helps.
    ball is a class and an array though....

  6. Post #6
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    Yikes I've never used Vectors. I appreciate the help but do they have to be used in this situation?

    Edited:

    ball is a class and an array though....
    Shit no that's making a new member of the class. I thought you could do that.

  7. Post #7
    Gold Member
    PvtCupcakes's Avatar
    May 2008
    10,900 Posts
    Capitalize your classes.

  8. Post #8
    Gold Member
    efeX's Avatar
    April 2009
    2,332 Posts
    Capitalize your classes.
    don't tell him what to do.

  9. Post #9
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    Yikes I've never used Vectors. I appreciate the help but do they have to be used in this situation?

    Edited:



    Shit no that's making a new member of the class. I thought you could do that.
    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.

  10. Post #10
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    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.

    Edited:

    Capitalize your classes.
    Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?

  11. Post #11
    Gold Member
    gilly_54's Avatar
    June 2006
    260 Posts
    Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?
    I'd say it's a generally well accepted syntax.

  12. Post #12
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    Also, what's with all the balls lately?
    Dude I fucking love balls.

  13. Post #13
    Gold Member
    Mr.123's Avatar
    February 2006
    255 Posts
    Also, what's with all the balls lately?
    Balls are fucking amazing.. Plus They are a easy to make shape :D

  14. Post #14
    Gold Member
    Deco Da Man's Avatar
    July 2007
    1,015 Posts
    Huh? I never capitalise them, are you supposed to? :S I mean is it good practise?
    Find a piece of good code that doesn't capitalise class names.

  15. Post #15
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    The C++ Standard Library.

  16. Post #16
    Gold Member
    Deco Da Man's Avatar
    July 2007
    1,015 Posts
    The C++ Standard Library.
    Good point, but I believe the STL classes can be seen more as base types, like int or float.

  17. Post #17
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    I beg to differ.
    Anyways, how about Boost? Or zlib?

  18. Post #18
    Gold Member
    Deco Da Man's Avatar
    July 2007
    1,015 Posts
    I beg to differ.
    Anyways, how about Boost? Or zlib?
    Let me rephrase:

    Show me a good piece of code that does not constitute a library or coding tool that doesn't capitalise it's classes.

  19. Post #19
    jefflol's Avatar
    August 2008
    24 Posts
    Basing syntax on an "inside joke" probably isn't a good idea, contrary to common FUCKING SENSE.

  20. Post #20
    blankthemuffin's Avatar
    July 2009
    1,265 Posts
    Let me rephrase:

    Show me a good piece of code that does not constitute a library or coding tool that doesn't capitalise it's classes.
    Show me how it's relevant.

  21. Post #21
    Gold Member
    ZeekyHBomb's Avatar
    June 2006
    3,577 Posts
    Let me rephrase:

    Show me a good piece of code that does not constitute a library or coding tool that doesn't capitalise it's classes.
    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.

    Edited:
    And I just looked at the Gimp source-code, it's only using lower-case characters, too.

  22. Post #22
    Gold Member
    Catdaemon's Avatar
    February 2005
    2,821 Posts
    Let me rephrase:

    Show me a good piece of code that does not constitute a library or coding tool that doesn't capitalise it's classes.
    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.

  23. Post #23
    Gold Member
    HubmaN V2's Avatar
    November 2007
    885 Posts
    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.
    Hell, they use Systems Hungarian there.

  24. Post #24
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    Basing syntax on an "inside joke" probably isn't a good idea, contrary to common FUCKING SENSE.
    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.

  25. Post #25
    Gold Member
    Vampired's Avatar
    February 2005
    371 Posts
    Code:
    if(usrinput.key.keysym.sym == SDLK_SPACE) { //If Space is pressed
        balls++; //Add a ball
        ball ball[balls];
        ball[balls].ballinit();
    }
    Why don't you use numballs or num_balls or numberOfBalls for the integer, Ball/ball for the class name and balls 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.

  26. Post #26
    jefflol's Avatar
    August 2008
    24 Posts
    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.
    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?

  27. Post #27
    Gold Member
    nos217's Avatar
    December 2006
    2,660 Posts
    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.