passing to a member function by reference?

This is a discussion on passing to a member function by reference? within the C++ Programming forums, part of the General Programming Boards category; I'm sure this is obvious to you smart types but. main has an array: Code: Bullet * bull[4]; and i'm ...

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    passing to a member function by reference?

    I'm sure this is obvious to you smart types but.

    main has an array:
    Code:
    Bullet * bull[4];
    and i'm trying to pass pieces of that array to the player object.

    Protoype:
    Code:
    void shoot(const Bullet &bull);  //line 37
    Code:
    void Player::shoot(const Bullet &bull){ //line 44
        if(bull != NULL){
            bull = new Bullet(loc.X+1, loc.Y,LEFT);
        }
    }
    and i'm getting:
    Code:
    37 C:\Program Files\Dev-Cpp\Projects\shootmain.cpp expected `,' or `...' before '&' token 
    37 C:\Program Files\Dev-Cpp\Projects\shootmain.cpp ISO C++ forbids declaration of `Bullet' with no type 
    45 C:\Program Files\Dev-Cpp\Projects\shootmain.cpp expected `,' or `...' before '&' token
     C:\Program Files\Dev-Cpp\Projects\shootmain.cpp In member function `void Player::shoot(int)': 
    46 C:\Program Files\Dev-Cpp\Projects\shootmain.cpp `bull' undeclared (first use this function) 
      (Each undeclared identifier is reported only once for each function it appears in.) 
    47 C:\Program Files\Dev-Cpp\Projects\shootmain.cpp `Bullet' is not a type 
     C:\Program Files\Dev-Cpp\Projects\Makefile.win [Build Error] exe: *** [Objects/MingW/shootmain.o] Error 1
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Usually that means that "Bullet" is not recognized as a type; hopefully you have your header file #included?

    Also: do you really propose to change a const Bullet reference inside your shoot function? And how can a Bullet object compare to NULL? Are you thinking that a reference is the same as a pointer?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    It looks like you're trying to pass the pointer by reference. It should not be const because you would be changing what the pointer points to:
    Code:
    void Player::shoot(const Bullet*& bull){ //line 44
        if(bull != NULL){
            bull = new Bullet(loc.X+1, loc.Y,LEFT);
        }
    }
    That doesn't fix the compiler errors, but it's how to do what it looks like you're trying to do. Note that you have to make the same change to the prototype.

  4. #4
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by tabstop View Post
    Usually that means that "Bullet" is not recognized as a type; hopefully you have your header file #included?

    Also: do you really propose to change a const Bullet reference inside your shoot function? And how can a Bullet object compare to NULL? Are you thinking that a reference is the same as a pointer?
    The Bullet class is declared lower in the program. See the Bullet pointer array holds 4 bullets (the max that can be on the screen) and when a bullet isn't active, that pointer is declared null.

    so what i'm trying to do is when the coditions are met bull[x] is passed to Player::shoot which checks to see if that bullet is gone and creates a new bullet in that slot...
    Sorry, but i'm a Code::Blocks man now.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Terran View Post
    The Bullet class is declared lower in the program.
    Lower in the program doesn't count. It has to be visible when the compiler gets to the prototype.

  6. #6
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    how does that make sense if you need:

    Code:
    class Z {
    
         void foo(X x) {};
         void bar(Y y) {};
    };
    class X {
    
          void foo( Y y) {};
          void bar(Z z) {};
    };
    
    class Y {
         
          .....
    
    };
    but my main problem is that i need to be able to pass one of the four bull[] arrays to Player::shoot so that i can create a new Bullet object in that array space.

    edit: i'm trying to pass it with
    Code:
    thePlay->shoot(&bull[1]);
    Last edited by Terran; 06-04-2008 at 07:03 PM.
    Sorry, but i'm a Code::Blocks man now.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Terran View Post
    how does that make sense if you need:

    Code:
    class Z {
    
         void foo(X x) {};
         void bar(Y y) {};
    };
    class X {
    
          void foo( Y y) {};
          void bar(Z z) {};
    };
    
    class Y {
         
          .....
    
    };
    but my main problem is that i need to be able to pass one of the four bull[] arrays to Player::shoot so that i can create a new Bullet object in that array space.
    If your function only takes a pointer, then you can use a forward declaration:
    Code:
    class X;
    
    class Y{
        void foo(X *x);
    }
    
    class X{
        //et cetera
    }
    This is fortunate for you, because despite yourself, you want to pass a pointer to your function. Remember, the things in your array are not Bullets, they are pointers to Bullets; so make the argument to your function a Bullet *& (you'll need the & since you are changing the value of the pointer inside the function, and since you're changing the value of the pointer, you can't make it const). Also, you don't have four bull arrays -- you have one bull array. (Unless you were trying to interpret each element of the array as a dynamically-allocated array, but then your shoot function would be really fubar, since you always allocate a single Bullet.)

  8. #8
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Ok it compiles, but even if that part of the array doesn't equal NULL, this still executes:

    Code:
    void Player::shoot(Bullet * &bull){
        if(bull != NULL){
           bull = new Bullet(loc.X+1, loc.Y,LEFT);
        }
        return;
    }
    Last edited by Terran; 06-04-2008 at 07:44 PM.
    Sorry, but i'm a Code::Blocks man now.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    The if block will only function if the pointer is not null, that's what != means. Maybe you meant something else?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  3. passing struct to function by reference
    By ironfistchamp in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 02:05 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21