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

  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Function
AI Thread 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"
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
4
Views
3K
Replies
3
Views
3K
Replies
1
Views
8K
Replies
4
Views
6K
Replies
1
Views
3K
Back
Top