-
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
-
Sounds familiar. Post the function and let's have a look at it.
-
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
-
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"
-
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
-
char get_cell(int row, int col) const {return grid[row][col];}
Notice the const after the parameter list
-
now i'm getting some darn unresolved external error..i think its too late for me tonight..i've gone retarded
-
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
-
its unresolved for another function...ahh, screw it ...haha
-
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
-
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