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

  • MHB
  • Thread starter Teh
  • Start date
  • Tags
    Function
In summary, in this conversation, the person is seeking help with their program and they mention being close to solving the problem. The expert summarizer does not respond to the questions but provides a summary of the conversation. The conversation also includes a code snippet and an error message, indicating that the person is trying to call a function with two arguments but the function has only been declared with no arguments. The expert summarizer also points out that the variables used as arguments do not necessarily have to be named arg1 and arg2.
  • #1
Teh
47
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
  • #2
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].
 
  • #3
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"
 

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

What is a function stub?

A function stub is a placeholder for a function that has not yet been fully implemented. It contains the name of the function, its parameters, and the expected return value, but does not contain any code for the function's logic.

Why are function stubs used?

Function stubs are used as a temporary placeholder when developing software. They allow developers to create a framework for their code and test it without having to write the full functionality of each function. This can save time and help with organization.

How do you create a function stub?

To create a function stub, you simply declare a function with the desired name, parameters, and return value, but leave the body of the function empty. For example:
function add(x, y) {
// function body will be added later
return null; // placeholder return value
}

Can you use function stubs in any programming language?

Yes, function stubs can be used in any programming language that supports functions. They are a common practice in object-oriented programming languages like Java, C++, and Python.

When should function stubs be replaced with actual code?

Function stubs should be replaced with actual code as soon as possible. They are meant to be temporary placeholders and should not be used in a final product. The purpose of function stubs is to help with development and testing, not to be part of the final program.

Similar threads

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