Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,988 people online right now. Registration is fast and FREE... Join Now!




[Linker Error]Undefined Reference

 
Reply to this topicStart new topic

[Linker Error]Undefined Reference

ligerzero459
6 Dec, 2007 - 07:32 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 5


My Contributions
I've been working on a program as a practice for my CSC 101 class. We're done with work, but I felt that I should practice some more on my own. I wrote a program that reads 10 decimals into an array, the calculates the sum and average, displays it on screen in reverse order and then displays the largest and smallest number in the array. I tried to do the largest and smallest with functions, but every time I try to compile it get the following errors

In function `main':
[Linker error] undefined reference to `Largest(float, int)'
[Linker error] undefined reference to `Smallest(float, int)'

I'm not sure what could be causing this, because I'm relatively new to C++. Here is the code for my program.

CODE
#include <iostream>
#include <iomanip>
using namespace std;

float Largest(float, int);
float Smallest(float, int);

const int ArraySize = 10;       ///sets ArraySize
float nums[ArraySize];

int main()
{
    float sum = 0.0;
    float avg = 0.0;
    
    cout << "Please enter " << ArraySize << " decimal numbers: ";
    for (int i = 0; i < ArraySize; i++)
        cin >> nums[i];
    
    //Calculate sum
    for (int j = 0; j < ArraySize; j++)
        sum += nums[j];
        
    //Calculate average
    avg = sum / float(ArraySize);
    
    //Display results
    cout << "\n\nThe array in reverse order:\n";
    for (int i = ArraySize - 1; i >= 0; i--)    //Reads array in reverse
        cout << setw(8) << nums[i];
    cout << endl << endl;
    
    //Sum & Average
    cout << "Sum: " << sum << endl;
    cout << "Average: " << avg << endl << endl;
    
    //Largest and Smallest numbers
    cout << "Largest Number: " << Largest(nums[ArraySize], ArraySize) << endl;
    cout << "Smallest Number: " << Smallest(nums[ArraySize], ArraySize) << endl;
    
    cout << endl;
    system("pause");
    return 0;
}

float Largest(float nums[ArraySize], int ArraySize)
{
      int index = 0;
      
      //compare numbers
      for (int i = 1; i < ArraySize; i++)
          if (nums[index] < nums[i])
             index = i;
            
      return nums[index];
}

float Smallest(float nums[ArraySize], int ArraySize)
{
      int index = 0;
      
      //compare numbers
      for (int j = 1; j < ArraySize; j++)
          if (nums[index] > nums[j])
             index = j;
            
      return nums[index];
}

User is offlineProfile CardPM
+Quote Post

ligerzero459
RE: [Linker Error]Undefined Reference
6 Dec, 2007 - 07:32 PM
Post #2

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 5


My Contributions
I've been working on a program as a practice for my CSC 101 class. We're done with work, but I felt that I should practice some more on my own. I wrote a program that reads 10 decimals into an array, the calculates the sum and average, displays it on screen in reverse order and then displays the largest and smallest number in the array. I tried to do the largest and smallest with functions, but every time I try to compile it get the following errors

In function `main':
[Linker error] undefined reference to `Largest(float, int)'
[Linker error] undefined reference to `Smallest(float, int)'

I'm not sure what could be causing this, because I'm relatively new to C++. Here is the code for my program.

CODE
#include <iostream>
#include <iomanip>
using namespace std;

float Largest(float, int);
float Smallest(float, int);

const int ArraySize = 10;       ///sets ArraySize
float nums[ArraySize];

int main()
{
    float sum = 0.0;
    float avg = 0.0;
    
    cout << "Please enter " << ArraySize << " decimal numbers: ";
    for (int i = 0; i < ArraySize; i++)
        cin >> nums[i];
    
    //Calculate sum
    for (int j = 0; j < ArraySize; j++)
        sum += nums[j];
        
    //Calculate average
    avg = sum / float(ArraySize);
    
    //Display results
    cout << "\n\nThe array in reverse order:\n";
    for (int i = ArraySize - 1; i >= 0; i--)    //Reads array in reverse
        cout << setw(8) << nums[i];
    cout << endl << endl;
    
    //Sum & Average
    cout << "Sum: " << sum << endl;
    cout << "Average: " << avg << endl << endl;
    
    //Largest and Smallest numbers
    cout << "Largest Number: " << Largest(nums[ArraySize], ArraySize) << endl;
    cout << "Smallest Number: " << Smallest(nums[ArraySize], ArraySize) << endl;
    
    cout << endl;
    system("pause");
    return 0;
}

float Largest(float nums[ArraySize], int ArraySize)
{
      int index = 0;
      
      //compare numbers
      for (int i = 1; i < ArraySize; i++)
          if (nums[index] < nums[i])
             index = i;
            
      return nums[index];
}

float Smallest(float nums[ArraySize], int ArraySize)
{
      int index = 0;
      
      //compare numbers
      for (int j = 1; j < ArraySize; j++)
          if (nums[index] > nums[j])
             index = j;
            
      return nums[index];
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: [Linker Error]Undefined Reference
6 Dec, 2007 - 10:44 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Got to remember that your declarations and the actual function signatures must match! You have your declarations saying that the functions take a float and an integer. Then down in the functions you have them take float ARRAYS.

The error is telling you that it sees declarations for functions that take a float and an integer and doesn't see the definitions because it is not matching your float array parameter to the float you put in the declaration.

Solution? Modify your declaration to include float [] instead of just "float". Secondly, change your calls to largest and smallest using just the array name, no subscript...

CODE

// Notice no subscripts on nums, just the name.
cout << "Largest Number: " << Largest(nums, ArraySize) << endl;
cout << "Smallest Number: " << Smallest(nums, ArraySize) << endl;


Once you make these corrections, the program works fine. Enjoy!

"At DIC we be declaration matching and array passing code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

ligerzero459
RE: [Linker Error]Undefined Reference
6 Dec, 2007 - 11:32 PM
Post #4

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 5


My Contributions
Wow, such a simple thing, yet I never would've known. Thanks. The program works perfectly now. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:55AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month