What is causing my undefined reference error for ComputeAvg()?

  • Context: MHB 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
SUMMARY

The forum discussion addresses an "undefined reference" error encountered in a C++ program related to the function ComputeAvg(). The user has defined ComputeAvg() without parameters, yet attempts to call it with two arguments in the main function, leading to a compilation error. The solution involves modifying the function prototype of ComputeAvg() to accept two integer parameters, thus resolving the undefined reference issue.

PREREQUISITES
  • Understanding of C++ function prototypes and definitions
  • Familiarity with basic C++ syntax and structure
  • Knowledge of how to handle function arguments in C++
  • Experience with debugging compilation errors in C++ programs
NEXT STEPS
  • Learn how to define and implement functions with parameters in C++
  • Research C++ error handling techniques for undefined references
  • Explore the use of function overloading in C++
  • Study best practices for structuring C++ programs to avoid common errors
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting function-related compilation errors in their code.

Teh
Messages
47
Reaction score
0
What is wrong with my program. I am so close!
Define stubs for the functions called by the below main(). Each stub should print "FIXME: Finish FunctionName()" followed by a newline, and should return -1. Example output:
Code:
FIXME: Finish GetUserNum()
FIXME: Finish GetUserNum()
FIXME: Finish ComputeAvg()
Avg: -1

#include <iostream>
using namespace std;

/* Your solution goes here  */

int GetUserNum();
int ComputeAvg();

int FunctionName(int argc, char ** v )
{
   GetUserNum();
   ComputeAvg();
   return 0;
}

int GetUserNum()
{
   cout << "FIXME: Finish GetUserNum()" << endl;
   return -1;
}

int ComputeAvg()
{
   cout << "FIXME: Finish ComputeAvg()" << endl;
   return -1;
}
 /* my solution ^ ^ ^ */

int main() {
   int userNum1 = 0;
   int userNum2 = 0;
   int avgResult = 0;

   userNum1 = GetUserNum();
   userNum2 = GetUserNum();

   avgResult = ComputeAvg(userNum1, userNum2);

   cout << "Avg: " << avgResult << endl;

   return 0;
}
output:

main.cpp: In function ‘int main()’:
main.cpp:42:45: error: too many arguments to function ‘int ComputeAvg()’
main.cpp:27:5: note: declared here
 
Technology news on Phys.org
In the prototype for[m]ComputeAvg()[/m] you need [m]int ComputeAvg(int arg1, int arg2)[/m]. The names for the variables don't necessarily have to be [m]arg1[/m] and [m]arg2[/m].
 
I am stuck on the same problem and am a bit confused as to how to do this. I have the stub function for GetUserNum done, but when I try and do ComputeAvg(userNum1,userNum2), the compiler tells me
"undefined reference to `ComputeAvg(int, int)'
collect2: error: ld returned 1 exit status"
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
12
Views
3K
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K