Swapping pointers

This is a discussion on Swapping pointers within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a piece of code to perform a bubble sort on an array of objects based on the ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Swapping pointers

    Hello,

    I have a piece of code to perform a bubble sort on an array of objects based on the object's name property. Unfortunaltely, the pointer swap in my bubble sort gives be error messages.

    The code below does the swapping inside my bubble sort:

    Code:
    //swap the layer pointers
    				//create a temporary layer pointer
    				Layer * tempPtr = MunicArg->pickLayer(p);
    				MunicArg->pickLayer(p) = MunicArg->pickLayer(p+1);
    				MunicArg->pickLayer(p+1) = tempPtr;
    Here is the code for my pickLayer function:

    Code:
    //begin pickLayer
    Layer * Munic::pickLayer(int i)
    {
    	//return the munic based on the index
    	return layers[i];
    
    }	//end County::pickLayers
    The array layers is an array of type Layer *.

    Here are the error messages:

    Code:
    c:\temp\c++\pec\pec\digObj.cpp(565): error C2106: '=' : left operand must be l-value
    c:\temp\c++\pec\pec\digObj.cpp(566): error C2106: '=' : left operand must be l-value
    Why can't I assign the return of pickLayer a value of another pointer?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > return layers[i];

    Try:
    return &layers[i];

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The most simple solution is to return a reference to a pointer.

    Code:
    //begin pickLayer
    Layer *& Munic::pickLayer(int i)
    {
    	//return the munic based on the index
    	return layers[i];
    
    }	//end County:ickLayers
    That will fix it without requiring any other changes (except in the class definition for return type of pickLayer).

    The reason it won't work is the same reason this won't work.

    Code:
    int someNum() { return 5; }
    
    int main() {
      someNum() = 3;
      return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    In your original function, you return a value, not the actual object. Hence, you get the error message saying that you are trying to assign something to a value.

    Just wondering though, since you are using OOP, why don't you just use a setvalue function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping pointers
    By csvraju in forum C Programming
    Replies: 17
    Last Post: 04-01-2009, 03:18 AM
  2. Swapping pointers
    By Mostly Harmless in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2008, 10:07 PM
  3. Swapping Pointers & Arrays
    By bartybasher in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2003, 02:17 PM
  4. Swapping string char with pointers
    By Black-Hearted in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2003, 05:36 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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