Thread: little vector problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    37

    little vector problem

    Hi peeps,

    I loop through my draughts in my game, and if that draught is in a forced move position then i store it in a vector. so draught 3 and 4 could be stored. Now when the player comes to make the move it checks if on of those draughts have been picked, if not its an invalid move. Now the problem is i don't think im comparing the draught that is selected to the one stored in the vector. I am using this line f code to do it:

    Code:
    if (sq1->getDraught() == move) {
    sq1 is the square that is selected by the player, then i get the draught on the square and see if the number of it is the same as the one as move (move is an iterator for my vector). I get a warning:

    Warning W8011 Game.cpp 320: Nonportable pointer comparison in function Game::mov
    e(Square *,Draught *)

    Any help in how to do this or if i have done anything wrong??

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> move is an iterator for my vector
    If move is an iterator to your vector, then that is definitely wrong. Are you using a vector<Draught*> or a vector<Draught>? Does getDraught() return a Draught* or a Draught?

    Assuming the first answer for both those questions, then you want to dereference the iterator to get the pointer it refers to:
    Code:
    if (sq1->getDraught() == *move) {
    If that's not what you are doing, then perhaps you can post at least the declarations of the variables and functions used, as well as a couple lines before and after the code for context.

    It looks like the warning thinks that you are comparing a Square* with a Draught*, although based on your description I'm not sure how that could happen in this line of code.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If move is an iterator, may-be it has to be dereferenced. Does getDraught return a pointer?

    Another approach would be:
    if the player selects a square (and the vector of forced moves is not empty), do a search in the vector (std::find in <algorithm>) to see if it contains this square. If not, the player is attempting an illegal move.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    My getDraught() returns this:

    Code:
    Draught * Square::getDraught()
    {
    	return draught;
    }
    for my vector, i get the number of draughts concerning the current player, then i for loop through each one:

    Code:
    for(int i=0; i<player1->getNDraughts(); i++){
    each one checks if there is a forced move, then i store it in my vector:

    Code:
    vector<int> forcedMovesp1;
    
    //this is where i make the vector for the draughts and add it like so:
    
    forcedMovesp1.push_back(i);
    if that helps.

    thanks

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, you are comparing a pointer to a Draught object with an iterator to a vector of ints? They are not going to be equal. If you dereference them, they might be comparable, if a Draught objects has an overloaded int operator.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    thanks for the help guys i got it working, i stored pointers in the vectors instead, noob mistake !

Popular pages Recent additions subscribe to a feed