Pass by Reference Function Build Error

  • Context: C/C++ 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Error Reference
Click For Summary
SUMMARY

The forum discussion centers on a build error encountered while implementing a pass by reference function in C++. The user mistakenly declared the function prototype for tempC2F_REF as void tempC2F_REF(double tempInCelsius, double returnByREF), which does not match its definition void tempC2F_REF(double tempC, double& TRef). This mismatch leads to a linkage error due to differing signatures. The solution is to correct the function prototype to void tempC2F_REF(double, double&) to align with the definition.

PREREQUISITES
  • Understanding of C++ function prototypes and definitions
  • Knowledge of pass by value and pass by reference concepts
  • Familiarity with basic file I/O operations in C++
  • Experience with debugging linkage errors in C++
NEXT STEPS
  • Review C++ function prototypes and their importance in avoiding linkage errors
  • Learn about the differences between pass by value and pass by reference in C++
  • Explore C++ file I/O operations using ifstream and ofstream
  • Investigate common C++ compilation and linkage errors for better debugging
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting function linkage errors in their code.

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:
 

Similar threads

Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · 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