Linker Error/Undefined Reference

This is a discussion on Linker Error/Undefined Reference within the C Programming forums, part of the General Programming Boards category; I know there's a million of these but I've been trying to work through it for a while and can't ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Linker Error/Undefined Reference

    I know there's a million of these but I've been trying to work through it for a while and can't grasp it.

    Basically have to make a calculator using functions for the basic mathematical operations.

    Here's a small bit of my code, and I'm getting the same error for each of my switch statements so one answer should work for all.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
         int add(int, int); //addition function prototype
         int subtract(int, int); //subtraction function prototype
         int multiply(int, int); //multiply function prototype
         int divide(int, int); //divide function prototype
         int modulus(int, int); //modulus function prototype
         int primeNumber(int); //test prime function prototype
         int power(int, int); //exponent function prototype
         int factorial(int); //factorial function prototype
    
    int main()
    {
          
          /*Declare/Initialize Variables*/
         int iSelection = 0; //Menu selection
         int iFirstDigit = 0;  //First digit entered
         int iSecondDigit = 0; //Second digit entered
         
    while (iSelection !=10)
    {
         
         /*Begin Calculator*/
         printf("\n                Calculator\n\n");
         
         printf("1   Addition\n");
         printf("2   Subtraction\n");
         printf("3   Multiplication\n");
         printf("4   Division\n");
         printf("5   Modulus (Integers Only)\n");
         printf("6   Test If Prime (Integers Only)\n");
         printf("7   Factorial (Integers Only)\n");
         printf("8   Power\n");
         printf("10  Exit Calculator\n");
         
         /*Prompt User To Begin Interaction*/
         printf("\nPlease select one of the options (1-7): ");
         scanf("&#37;d", &iSelection);
    
    
         switch(iSelection) //begin switch
         {
              case 1:
                     printf("\nPlease enter first digit to be added: ");
                     scanf("%d", &iFirstDigit);
                     printf("\nPlease enter second digit to be added: ");
                     scanf("%d", &iSecondDigit);
                     printf("\nThe sum of the two numbers is %d\n\n", add(iFirstDigit, iSecondDigit));
         
              int add (int iFirstDigit, int iSecondDigit)
                   {
                      return iFirstDigit + iSecondDigit;
                   }
                   break; //end case 1
    My compile error is

    [Linker error]: undefined reference to 'add'



    I'm assuming it has something to do with how I've defined/prototyped my function or something along those lines, but I can't spot the error myself.

    I've looked through past threads of this nature and couldn't really find the solution. Any help at all is appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yes, you have to write those functions add(), subtract(), etc. I think you've started doing that with the add() function - but it's embedded inside the case statement. You need to move that code outside of the main(). At the top I would put them. Then they act as prototypes and you don't have a need for the prototype statements. Or you can place the functions AFTER the main. Then you DO need the prototype statements.
    Code:
    int add (int iFirstDigit, int iSecondDigit)
                   {
                      return iFirstDigit + iSecondDigit;
                   }
    
    int subtract...
    
    int main()
    {
          
     ...
    Last edited by nonoob; 09-24-2008 at 08:54 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    Thank you thank you

    So simple -.-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21