Thread: Parameter Passing

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    14

    Parameter Passing

    Hello Everyone,

    I couldn't whip up a reasonable title for this post, so I just used the most broad title.

    Currently, I am writing a class for a singly linked list, which by all means, is pretty straightforward. I am going to post code first, then explain.

    In my abstract class, I have this function prototype (there are others, but for this purpose there are no reasons to show more)

    Code:
       
    // Get AND DELETE the first element of the list, placing it into the// return variable "value".  If the list is empty, return false, otherwise
    // return true.
      virtual bool getfirst(Elem &returnvalue) = 0;
    Elem is the name of the "typename or class" of the template. It holds ints/doubles. The functionality is

    In my main, I have this to test my function. Below, you see the variable value. It is simply just an int with a default value (i.e. int value; )

    Code:
       // Print the list by repeatedly getting the first value  cout << "Using getfirst():" << endl;
      while (mylist.getfirst(value)) 
        cout << value << ", ";
      cout << endl;
    And finally, here is my implementation of the getfirst function

    Code:
    template <class Elem>
    bool LinkedSortedList<Elem>::getfirst( Elem &returnvalue )
    {
        LinkedNode<Elem>* tp = head; //temp pointer, starts at front of list
    
    
        if( tp != NULL ) //if list is not empty
        {
            returnvalue = tp->value; //set the first item to returnvalue
            head = tp->next; //update head pointer
            delete tp; //delete node
            count--; 
            return true;
        }
        else
        {
            return false;
        }
    }

    Now, while running the test in main, this is supposed to return true, which it does, but it does not "return" the "return value".

    I am passing the value by reference (address), so yes, I should be able to change the value permanently.

    Also, I've tried using returnvalue = &tp->value but I get a compiler error : error C2440: '=' : cannot convert from 'int *' to 'int'
    error C2440: '=' : cannot convert from 'double *' to 'double'

    And finally, since this is a template, I instantiated two lists at the bottom of my implementation file. (e.g. LinkedSortedList<int> IntNeverUsed; )

    mrJT

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Except for the title this is a pretty good post so I'll try to help, but I have to ask, why did "reference parameter not updating variable" not cross your mind?

    You've marked the function as purely abstract (= 0) but still defined it; this is almost certainly an error.

    The `getfirst' function has succumb to feature creep; it does more than it should.

    The name `getfirst' is clearly wrong; I expect that I could examine a value without destroying it.

    Beyond that, the actual use of references seems correct but because of the wording you've used and examples you've provided I can't help but assume that you aren't getting a clean compile of the new code but are instead assuming that the code is compiling properly and testing the executable of an old version.

    Soma

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    14
    You've marked the function as purely abstract (= 0) but still defined it; this is almost certainly an error.
    - All of these functions are in separate files. When making a function purely virtual, you must include the implementation of the function in the class that is deriving the abstract class.

    The `getfirst' function has succumb to feature creep; it does more than it should.
    - getfirst is doing exactly what it is intended to do. Take the first element in the list, assign it to returnvalue, and return true. If list is empty, return false. The problem is, when invoking getfirst from the main and passing it an int by reference, it does not change the value of that int (in this case, int is returnvalue)

    - getfirst is operating on a sorted linked list. Sorted, as in ascending order (smallest to greatest).

    Beyond that, the actual use of references seems correct but because of the wording you've used and examples you've provided I can't help but assume that you aren't getting a clean compile of the new code but are instead assuming that the code is compiling properly and testing the executable of an old version.
    - It is compiling just fine. What I am saying is, when trying to use the address of tp->value (e.g. &tp->value) and storing it into returnvalue, I get the error. ( I included this because it was my cheap attempt at solving my problem ).

    mrJT

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The problem is, when invoking getfirst from the main and passing it an int by reference, it does not change the value of that int
    Actually, from what you've posted, that is probably not the problem. You only see evidence and place blame.

    *shrug*

    Unfortunately, I can only make assumptions until you actual give more to go on so...

    My next assumption is: Pinkie Pie.

    Yes. Pinkie Pie.

    Why? Because Pinkie Pie is awesome!

    Soma

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Problems like these are what a debugger is for. Step through the getfirst function with a debugger and find out what instructions it actually runs. You may be surprised.

    I agree with Soma though: getfirst() should be called popfirst() or pop_front(), to keep with the standard container nomenclature.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing (...) as parameter
    By simpatico_qa in forum C Programming
    Replies: 10
    Last Post: 03-27-2009, 05:05 PM
  2. parameter passing..??
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2008, 09:46 PM
  3. parameter passing?
    By theLukerBoy in forum C Programming
    Replies: 3
    Last Post: 11-03-2002, 06:25 PM
  4. parameter passing
    By Jax in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 05:24 AM
  5. Passing a parameter into the executable
    By neva4getme in forum C Programming
    Replies: 2
    Last Post: 10-11-2001, 01:18 PM