Solved

Linker Errors - C++ Multifile Program

Posted on 2006-11-06
5
326 Views
Last Modified: 2008-03-06
I've written a multifile program based on a header file, golfHeader.h. I'm running it on Bloodshed compiler. However, I'm getting linker errors when I try to run the main program (golfDriver) as follows: [Linker error] undefined reference to 'setgolf [golf&]';
[Linker error] undefined reference to 'showgolf [golf const &]'; [Linker error] undefined reference to 'setgolf [golf&, char const", int]'; [Linker error] undefined reference to 'show golf [golf const&]'; [Linker error] undefined reference to 'handicap [golf&, int]'; [Linker error] undefined reference to 'showgolf [golf const&]'.

Can someone help me with why I'm getting this specific error regarding my functions ?

---------------------------HERE IS MY CODE FOR THE PROGRAMS------------------------------

//MY HEADER FILE

// golfHeader.h
const int Len = 40;
struct golf
{
    char fullname[Len];
    int handicap;
};

// non-interactive version
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);

// interactive version
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

// function displays contents of golf structure
void showgolf(const golf & g);


----------------------------------------------------------

//MY FUNCTION DEFINITIONS

//File Name: golfDefinitions.cpp
//golfHeader.h -  (golfDriver.cpp)
#include <iostream>
#include "golfHeader.h"
#include <cstring>

//Function 1
int setgolf(golf & g)
{
    std::cout << "What is the golfer's name? ";
    std::cin.getline(g.fullname, Len);
    if (g.fullname[0] == '\0')
        return 0;                
    std::cout << "What is the golfer's handicap? " << g.fullname << ": ";
    while (!(std::cin >> g.handicap))
    {
        std::cin.clear();
        std::cout << "Entry must be an integer: ";
    }
    while (std::cin.get() != '\n')
        continue;
       
    //Pause system output
    system ("pause");    
       
    return 1;
}

// Function 2
void setgolf(golf & g, const char * name, int hc)
{
    std::strcpy(g.fullname, name);
    g.handicap = hc;
}

// Function 3
void handicap(golf & g, int hc)
{
    g.handicap = hc;
}

// Function 4
void showgolf(const golf & g)
{
    std::cout << "Golfer Named :   " << g.fullname << "\n";
    std::cout << "The Golfer's Handicap is : " << g.handicap << "\n\n";
}


----------------------------------------------------------

//MY DRIVER FILE (MAIN)

// File Name: golfDriver.cpp
#include <iostream>
#include "golfHeader.h"
// link with Function Definitions @ golfDefinitions.cpp
const int Mems = 5;
int main(void)
{
    using namespace std;
    golf team[Mems];

    cout << "Enter maximum " << Mems << " team members:\n";
    int i;
    for (i = 0; i < Mems; i++)
        if (setgolf(team[i]) == 0)
            break;
    for (int j = 0; j < i; j++)
        showgolf(team[j]);
    setgolf(team[0], "Tiger Woods", 5);
    showgolf(team[0]);
    handicap(team[0], 3);
    showgolf(team[0]);
   
    //Pause system output
    system ("pause");    

    return 0;
}



0
Comment
Question by:computerese
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17886597
How exactly are you compiling that? It seems that you forgot to compile with 'golfDefinitions.cpp', e.g.

g++ -o golfDriver.exe golfDriver.cpp golfDefinitions.cpp

should work.
0
 

Author Comment

by:computerese
ID: 17886644
Thanks jkr.

Where exactly might the code, " g++ -o golfDriver.exe golfDriver.cpp golfDefinitions.cpp," go?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 17886646
That's not code, but how you call your compiler. Again: How exactly are you compiling that?
0
 

Author Comment

by:computerese
ID: 17886772
Thanks jkr.
I was able to deduce what I needed to change based on your input. I simply put the code, "#include "golfDefinitions.cpp" in the driver file. All seems to be working good now.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17886789
That can work, but is not what I meant. You should rather specify the other files as additoinal compilation units when building your program.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now