i'm stuck on a const &

This is a discussion on i'm stuck on a const & within the C++ Programming forums, part of the General Programming Boards category; okay i dunno if you can help me cause my program is quit bit.. its the game battleship..its got two ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    i'm stuck on a const &

    okay i dunno if you can help me cause my program is quit bit..
    its the game battleship..its got two classes (ocean and ship)

    anyways..i am and error away from having it compile..

    i was wondering if any of you have gotten a similar error...if not..its okay...i will ask the teach tomorrow, but i've been workin at it for 6 hours today and wanna see my results..

    the error states...

    'ocean::get_cell' : cannot convert 'this' pointer from 'const ocean' to 'ocean&'

    get_cell is an accessor function that returns the row and column placement of a certain call.

    i dunno how else to be specific...

    thanks

    BBNERD

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Sounds familiar. Post the function and let's have a look at it.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    there are two potential functions we could have problems w/...


    the get_cell function is a public member function...

    i have it like this in my ocean.h file...

    char get_cell(int row, int col) {return grid[row][col];}

    the file that is bringing up the error when i click on the error is this public member function for in the ship.cpp file..

    bool ship::check_space(char dir, int col, int row, const ocean & field)
    {
    if(dir == 'v') // place vertical
    {
    for(int i(0); i<length; i++)
    if(field.get_cell(row+i, col) != '~')
    return false;
    }
    else // place horizontal
    {
    for(int i(0); i<length; i++)
    if(field.get_cell(row, col+i) != '~')
    return false;
    }
    return true;



    }

    its brings up errors for both times when i call the field.get_cell function....

    this is confusing for me...and i know the program...so i guess helping me would be hard...but hey its worth a shot!

    BBNERD

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You're trying to use a constant reference to an object when you need a non-cost reference, just like it says.

    The function most-likely is declared to be a const member functio and returns a reference to a constant object (check the declaration). Since you attempt to return the this pointer and the member function is declared as const, then it realizes that you don't want to do this!

    Either you should change the const member funciton to non-const, or change the return type to const ocean&, or make 2 versions of the function -- one for const and one for not.

    EDIT: I replied before you posted your other reply



    The problem is in your check_space function you have a parameter that's a constant reference to an ocean, but later on you try to use the get_cell function on it.

    You have to make get_cell a const member function

    so -- after the declaration and the header part of the get declaration definition write the word "const"
    Last edited by Polymorphic OOP; 12-03-2002 at 10:32 PM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    can you illustrate that in my code? i'm a lil dumb when it comes to following you on that...ahh..its been a long day...

    BBNERD

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    char get_cell(int row, int col) const {return grid[row][col];}

    Notice the const after the parameter list

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    now i'm getting some darn unresolved external error..i think its too late for me tonight..i've gone retarded

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    That's because you have to change the declaration as well, not just the definition.

    So add const after the argument list in the declaration as well!

    EDIT: Unless you're getting the error in another part of the code, which is more likely because it looks like your definition was inlined in the class definition
    Last edited by Polymorphic OOP; 12-03-2002 at 10:34 PM.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    its unresolved for another function...ahh, screw it ...haha

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    That just means that you're declaring a function but never defining it.

    For instance you might do something like:

    Code:
    void SomeFunction();
    
    int main()
    {
        SomeFunction();
        return 0;
    }
    It'll compile fine, but won't link because you never defined SomeFunction, just declared it

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    well i got it running now...but it doesn't work right..but hey, back to the drawing board..

    THANK YOU FOR THE HELP...this board is great

    BBNERD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 12:06 PM
  4. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  5. vector<>
    By teval in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2003, 03:27 PM

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