Pass by Reference Function Build Error

In summary, the programmer defined functions are not working properly because the value of a function parameter is being changed by reference instead of by value.
  • #1
Saladsamurai
3,020
7
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:
/*----------------------------------------------------
 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
	fin.open("input.txt");
	// make sure fin opened properly
	if (!fin)
	{
		cerr << "Could not open input file ... \n";
	}
	//open output file
	fout.open("output.txt");
	
	// 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;
		// PASS BY VALUE CONVERSION
		TVal = tempC2F_VAL(tempC);
		// PASS BY REFERENCE CONVERSION
		tempC2F_REF(tempC, TRef);
		fout << setw(5) << tempC
			 << setw(20) << TVal
			 << setw(25) << TRef
			 << endl;
	}
	
	// close all files and exit
	fin.close();
	fout.close();
    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;
	return;
}


Here is the build error I am getting:

Screenshot2012-01-02at62128PM.png


Here is a sample input file:

Code:
0
5
10
15
20
25
30
35
40
45
50
 
Technology news on Phys.org
  • #2
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.
 
  • #3
D H said:
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.

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:
 

1. What is the difference between pass by reference and pass by value in C++?

Pass by reference means passing the address of a variable to a function, while pass by value means passing the actual value of a variable to a function. This means that changes made to the variable within the function will affect the original variable in pass by reference, but not in pass by value.

2. Why is pass by reference preferred over pass by value in C++?

Pass by reference is preferred because it allows for more efficient memory usage and faster execution time. This is because it does not create a copy of the variable, but instead directly works with the original variable.

3. What is a "reference variable" in C++?

A reference variable in C++ is a variable that acts as an alias for another variable. It is declared using an ampersand (&) before the variable name, and it allows for pass by reference to be used in functions.

4. How do you pass an array by reference in C++?

To pass an array by reference in C++, the function parameter must be declared as a reference variable using the ampersand (&) symbol. This will allow changes made to the array within the function to be reflected in the original array.

5. Can you pass built-in data types by reference in C++?

Yes, built-in data types such as int, float, and char can be passed by reference in C++. This allows for more efficient memory usage and allows for changes made to the variable within the function to be reflected in the original variable.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Programming and Computer Science
Replies
8
Views
7K
  • Science and Math Textbooks
Replies
1
Views
923
  • Programming and Computer Science
Replies
2
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
12K
Back
Top