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

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function
Click For Summary
The discussion centers on a programming issue related to defining function stubs and resolving function signatures in C++. The user is attempting to implement two functions: GetUserNum() and ComputeAvg(). The main problem arises when calling ComputeAvg() with two arguments, as the function is currently defined without parameters, leading to a compilation error. The solution involves modifying the ComputeAvg() function to accept two integer parameters, which should be reflected in both its declaration and definition. Additionally, the user has successfully created a stub for GetUserNum() but is confused about how to properly define ComputeAvg() to avoid the "undefined reference" error. The output error messages indicate that the function prototype does not match the function call, highlighting the need for consistent function signatures in the 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"
 
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 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
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