Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++: Pass by Reference Error

  1. Jan 2, 2012 #1
    Hello all :smile:

    I am trying to sort out how to use pass by reference properly and I am getting a Build error related to my pass by reference function. Perhaps someone could help me out here. I thought I was doing this correctly by reading my text, but apparently not. Maybe someone can point out if I am missing the obvious here:

    Code (Text):

     Examples of some simple programmer-defined functions
     The 'main ()' function will make calls to 2 programmer
     defined functions: 1) that will be simple PASS BY VALUE
     which is the C++ default and 2) a PASS BY REFERENCE that
     is used to change the value of a function parameter. The
     functions will operate on values in a .txt file named
     input.txt and will send output to a file named
     output.txt ... how clever ...

    #include <iostream> // required for cin, cout, cerr
    #include <fstream>  // required for ifstream, ofstream
    #include <iomanip>  // required for setw
    using namespace std;

    // function prototypes
    double tempC2F_VAL(double tempInCelsius);
    void tempC2F_REF(double tempInCelsius, double returnByREF);

    // main program
    int main ()
        // declare variables in scope of main()
        double TVal, TRef(0), tempC;
        ifstream fin;
        ofstream fout;
        // bind fin to input.txt
        // make sure fin opened properly
        if (!fin)
            cerr << "Could not open input file ... \n";
        //open output file
        // print header
        fout << setw(5) << "Celsius"
             << setw(20) << "F-Pass by VALUE"
             << setw(25) << "F-Pass by REF"
             << endl;
        while (!fin.eof())
            // read in values and convert
            fin >> tempC;
            TVal = tempC2F_VAL(tempC);
            tempC2F_REF(tempC, TRef);
            fout << setw(5) << tempC
                 << setw(20) << TVal
                 << setw(25) << TRef
                 << endl;
        // close all files and exit
        return 0;

     1) A PASS BY VALUE function
     Converts temperature in Celsius to Fahrenheit
     Precondition:  tempC holds temp in Celsius
     Postcondition: returns temp in Fahrenheit

    double tempC2F_VAL(double tempC)    //function header
        // declare local variables
        double tempF;
        // convert
        tempF = tempC *(9.0/5.0) + 32.0;
        // return value in Fahrenheit
        return tempF;

     2) A PASS BY REFERENCE function
     Converts temperature in Celsius to Fahrenheit
     Precondition:  tempC holds temp in Celsius
     Postcondition: value of TRef in main() has been updated
     Note that function return is of type 'void' since it
     doe not 'return' anything. It augments TVal directly

    void tempC2F_REF(double tempC, double& TRef)    //function header
        // convert
        TRef = tempC *(9.0/5.0) + 32.0;


    Here is the build error I am getting:


    Here is a sample input file:

    Code (Text):


  2. jcsd
  3. Jan 2, 2012 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You declared the function as void tempC2F_REF(double tempInCelsius, double returnByREF). When the compiler gets to the call in main(), all the compiler knows about is this declared (but not defined) function tempC2F_REF(double,double). Later you define the function void tempC2F_REF(double tempC, double& TRef). This function has a different signature than the declared function, so it is a different function. You never defined the function with signature tempC2F_REF(double,double), so you got a linkage error.

    Fix your declaration to match the definition and all will be well.
  4. Jan 2, 2012 #3
    I see now! I need to have void tempC2F_REF(double, double& ) in my function prototype. I unfortunately followed along with an example from my text in which they left the prototype as type functionName(type, type) and the actual function definition was type functionName(type& var1, type& var2). I should have investigated that before posting since it struck me as odd at first. I got lost in it all somewhere along the way...

    Thanks again D H :smile:
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook