?
Solved

Linker Errors - C++ Multifile Program

Posted on 2006-11-06
5
Medium Priority
?
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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