Solved

Linker Errors - C++ Multifile Program

Posted on 2006-11-06
5
333 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 62
mixing C++ & C# in Vis Studio 2013 7 198
How do i run a c++ file? 15 46
C++ Code Issue 4 25
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

856 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