C/C++ Pass by Reference Function Build Error

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Error Reference
Click For Summary
The discussion revolves around a programming issue related to using pass by reference in C++. The user is encountering a build error due to a mismatch between the function prototype and its definition. The function `tempC2F_REF` is declared with a parameter of type `double`, but the definition uses a reference type `double&`. This discrepancy leads to a linkage error because the compiler sees these as different functions. The solution involves correcting the function prototype to match the definition by changing it to `void tempC2F_REF(double, double&)`. The user acknowledges the mistake and recognizes the importance of ensuring consistency between function declarations and definitions.
Saladsamurai
Messages
3,009
Reaction score
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
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.
 
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:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
9K
  • · Replies 8 ·
Replies
8
Views
7K