• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Linker Errors - C++ Multifile Program

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
computerese
Asked:
computerese
  • 3
  • 2
2 Solutions
 
jkrCommented:
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
 
computereseAuthor Commented:
Thanks jkr.

Where exactly might the code, " g++ -o golfDriver.exe golfDriver.cpp golfDefinitions.cpp," go?
0
 
jkrCommented:
That's not code, but how you call your compiler. Again: How exactly are you compiling that?
0
 
computereseAuthor Commented:
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
 
jkrCommented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now