Thread: &reference operator folly

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    &reference operator folly

    SeaCreature is a class (Contains XPos,YPos, and Sprite, where Sprite is an instance of a class with the listed values and more.)

    PHP Code:

    void SeaCreature
    ::CheckPosition(const SeaCreature &enemy)
    {
        
    SDL_Rect thisRectenemyRectcolRect;
        
        
    thisRect.XPos;
        
    thisRect.YPos;
        
    thisRect.Sprite.SpriteWidth();
        
    thisRect.Sprite.SpriteHeight();

                       
    enemyRect.enemy.XPos;
        
    enemyRect.enemy.YPos;
        
    enemyRect.enemy.Sprite.SpriteWidth();
        
    enemyRect.enemy.Sprite.SpriteHeight();
                    ...
    }

    the lines
        
    enemyRect.enemy.Sprite.SpriteWidth();
        
    enemyRect.enemy.Sprite.SpriteHeight();
    gave me two compile-time errors
    c:\usrsrc\cpp\cozSprite\SeaCreature.cpp(89) : error C2662: 'cozSprite::SpriteHeight' : cannot convert 'this' pointer from 'const cozSprite' to 'cozSprite &'


    I need to be able to modify enemy (if at all possible, so i used the & symbol)

    Thanks.. I've gotta get some rest

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    if I remember right this is the problem:
    Code:
    void SeaCreature::CheckPosition(const SeaCreature &enemy)
    const makes the SeaCreature unchangable while & means you want to be able to change it. So take out const

    cheers
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    i've never seen any const values being passedinto a function through hook or crook. THat's an interesting practice, i guess it comes in handy when you are coding in a group and you don't want anyone to mess with the value of a variable that you passed through a function.

Popular pages Recent additions subscribe to a feed