Problems with arrays and pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BSCode266
    New Member
    • Jan 2007
    • 38

    Problems with arrays and pointers

    Hi,

    This is some code in the first class:

    Code:
    //the array of pointers
    Slot *slots[2];
    
    //the get method for the previous array
    //The first error occurs here:  error C2440: 'return' : cannot convert from 'Slot *[2]' to 'Slot *'
    Slot* ForgeScreen::getSlots(){
    	return slots;
    }
    Slot is another class i defined of which i want 2 objects.

    Note that this is an array of pointers.

    Code:
    //This method has to return a pointer to a Slot.
    Slot* Controller::checkMousePosition(int x, int y){
    //slotArray is the pointer which will point to the array we are gonna get from the previous class.
    	Slot *slotArray;
    	if(inventory->getDraw()){
    		if( ( x > inventory->getX() ) && ( x < inventory->getX() +                                                                    inventory->getW()) && ( y > inventory->getY()) && ( y < inventory->getY() + inventory->getH()) ){
    //Here we get the array and make the slotArray point to it.
    				slotArray = inventory->getSlots();
    		}
    	}
    	if(forgeScreen->getDraw()){
    		if( ( x > forgeScreen->getX() ) && ( x < forgeScreen->getX() + forgeScreen->getW()) &&
    				( y > forgeScreen->getY()) && ( y < forgeScreen->getY() + forgeScreen->getH()) ){
    //Or here we get the array and make the slotArray point to it.
    					slotArray = forgeScreen->getSlots();
    		}
    	}
    
    //Its all about this part. 
    
    	if(slotArray != NULL){
    		for(int i = 0; i < 2; i++){
    			if( ( x > slotArray[i]->getX() ) && ( x < slotArray[i]->getX() + slotArray[i]->getW()) &&
    				( y > slotArray[i]->getY()) && ( y < slotArray[i]->getY() + slotArray[i]->getH()) ){
    //The second error occurs here: error C2440: 'return' : cannot convert from 'Slot' to 'Slot *'
    					return slotArray[i];
    			}
    		}
    	}
    	return NULL;
    }
    For some reason, which is unknown to me, slotArray[i] is an object instead of what i think it should be a pointer. So my compiler keeps asking me to change the -> to a . and also i get an error converting from a Slot to a Slot*. However i really do need a Slot * and not a Slot.

    I'd like to know:
    1. Why slotArray[i] is an object and not a pointer.
    2. How to solve the first problem(ofcours e)
    3. How to solve the 2nd problem

    Thanks in advance,

    BSCode266
  • BSCode266
    New Member
    • Jan 2007
    • 38

    #2
    It seems i already found the answer, for people who are searching for the same answer here is the solution:

    Code:
    //Before this was Slot* instead of Slot**, meaning that i was conferting it.
    Slot** ForgeScreen::getSlots(){
    	return slots;
    }
    Code:
    //Before this was Slot* instead of Slot**, this has to change because the return value of the get method has changed and we dont want to make the first mistake again.
    Slot** slotArray;
    Greetings,

    BSCode266

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      BSCode266]
      Slot** slotArray;
      [/quote]

      That is not an array. That is a a pointer to a Slot*. A Slot* is a pointer to a single Slot.

      Read this article: http://www.thescripts.com/forum/thread772412.html.

      Comment

      Working...