Initialising reference params

This is a discussion on Initialising reference params within the C++ Programming forums, part of the General Programming Boards category; This is a class that needs to return a value (result) by reference as this value indicates the error number ...

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Initialising reference params

    This is a class that needs to return a value (result) by reference as this value indicates the error number if the member functions return false... or the actual result if the member functions return true.

    When a RpnEvaluator object is created, it is passed a character string (to be evaluated) and a result.... this result variable has to be a reference param (for the reasons stated)

    This is the function that creates the object...


    Code:
    bool evaluate(char* expr, int &result) {
       RpnEvaluator Z(expr, result);
       return (Z.Evaluate());
    }
    The first line cannot be changed as the main function which calls this is the one that acts on the result variable (which is why it's passed by reference)... I can't change the main function or how this function is called.

    I cannot for the life of me figure out how to pass 'result' to my class so that it's member functions act on the original variable declared in main and not a copy... the closest I think I got gave me this error message...

    In method `RpnEvaluator::RpnEvaluator(char *, int &)':
    warning: uninitialized reference member `RpnEvaluator::result'
    But then I couldn't figure out how to initialise it to zero... didn't like being passed an int.

    Any help would be greatly appreciated... thanks...

  2. #2
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    No one knows? I find that hard to believe given the wealth of knowledge I've found here previously...

    I've got around this particular problem simply by passing the value by reference to the member functions that need to change it... but there must be a way to initialise a variable (reference or not) to point at the address of a reference parameter given to the constructor?

  3. #3
    Unregistered
    Guest

    bla testing___!

    Code:
     haha [tag]haha[/tag]
    [source] YES! [/source]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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