Thread: Undefined Reference Problem

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    Question Undefined Reference Problem

    I can't seem to find out the problem here. If anyone could help me, that would be great.

    Compiler output:
    c:\windows\TEMP\ccTo2Afb.o(.text+0x6b):histog~1.cp p: undefined reference to `average_numbers(int *, int)'
    c:\windows\TEMP\ccTo2Afb.o(.text+0xd0):histog~1.cp p: undefined reference to `make_frequency(int *, int, int *, int)'
    c:\windows\TEMP\ccTo2Afb.o(.text+0xe4):histog~1.cp p: undefined reference to `make_histogram(int *, int)'

    Program:

    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <fstream.h>

    const int MAX_ELMNTS = 100;
    const int ANLYS_RNG = 19;

    // ===========Open input file===========
    // Gets the name of the input file and opens it for input

    void open_input_file ( ifstream &in_file );
    // =============Get Data============
    // Read data from the file into the array. The array may not be completely
    // filled. It will use what is there and discard any extra data.

    int get_data
    ( int numbers[], int size, int range );

    // ============Print Data=============
    // Print the data as a 2-dimensional array.

    void print_data
    ( const int numbers[], int size, int line_size );

    // ============Average Numbers===========
    // Reckons the average of the numbers in the array.

    float average_numbers
    ( int numbers[], int size);

    // ===========Make Frequency==========
    // Puts the frequency count into an array.

    void make_frequency
    ( int numbers[], int size, int frequency[], int range );

    // ==============Make histogram========
    // Prints the histogram based on the frequency counts.

    void make_histogram
    ( int frequency[], int range );

    // ===============main==============
    int main()
    {
    // Local Declarations
    int size;
    int nums[MAX_ELMNTS];
    int frequency[ANLYS_RNG + 1];

    // Statements
    size = get_data( nums, MAX_ELMNTS, ANLYS_RNG );

    cout << "The average of the numbers is "
    << average_numbers( nums, size )
    << endl;

    print_data( nums, size, 10 );

    make_frequency( nums, size, frequency, ANLYS_RNG );
    make_histogram( frequency, ANLYS_RNG );

    system("PAUSE");
    return 0;
    }

    // ==========Open Input File===============
    void open_input_file
    ( ifstream &in_file )
    { char in_file_name[80];

    cout << "Enter the input file name: ";
    cin >> in_file_name;

    in_file.open(in_file_name);
    if (!in_file)
    cerr << "Error opening file\a\a\n", exit(100);
    }
    // ===========Average Numbers=============
    float average_numbers
    ( const int numbers[], int size )
    { float sum = 0.0;
    int i;

    for (i = 0; i < size; i++ )
    sum = float( numbers[i] ) + sum;
    if (size != 0)
    { return (sum/float(size)); }
    else
    { cout << "No values entered \n";
    exit (101);
    }
    }
    // =============Get Data===============
    int get_data
    ( int numbers[], int size, int range )
    {
    int data_in;
    int loader = 0;
    ifstream fs_data;

    open_input_file (fs_data);

    while (loader < size && (fs_data >> data_in))
    {
    if (data_in >= 0 && data_in <= range)
    numbers[loader++] = data_in;
    else
    cout << "Data point " << data_in
    << " invalid. Ignored. \n";
    }

    if (loader == size && (fs_data >> data_in))
    cout << "\nToo much data. Will process what was read. \n";
    return loader;
    }
    //==============Print Data===============
    void print_data
    ( const int numbers[], int size, int line_size )
    {
    int i;
    int num_printed = 0;

    cout << endl << endl;

    for (i = 0; i < size; i++)
    {
    num_printed++;
    cout << setw(4) << numbers[i];
    if (num_printed >= line_size)
    {
    cout << endl;
    num_printed = 0;
    }
    }
    cout << endl << endl;
    return;
    }
    //==============Make Frequency==========
    void make_frequency
    ( const int numbers[], int size, int frequency[], int range )
    {
    int i;

    for (i = 0; i <= range; i++)
    frequency[i] = 0;
    for (i = 0; i <= size; i++)
    frequency[numbers[i]] = frequency[numbers[i]] + 1;

    return;
    }
    //=============Make Histogram============
    void make_histogram
    ( const int frequency[], int range )
    {
    int i;
    int j;

    for (i = 0; i <= range; i++)
    {
    cout << setw(3) << i << setw(3) << frequency[i];
    for (j = 1; j <= frequency[i]; j++)
    cout << "*";
    cout << endl;
    }
    return;
    }
    Last edited by esilja; 11-08-2001 at 12:05 AM.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It's because the parameter types in your declarations don't match the parameter types in your definitions, so the compiler is looking for the definitions but can't find them.

    'const int numbers[]' is not the same as 'int numbers[]'.
    zen

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. Compiling Tutorial program with Dev-c++
    By h3ro in forum C++ Programming
    Replies: 15
    Last Post: 10-24-2006, 03:02 AM
  3. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 08:29 PM
  4. Linking problem - undefined reference to load_parameters
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:28 AM
  5. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM