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"
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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