Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linker Errors - C++ Multifile Program

Posted on 2006-11-06
5
Medium Priority
?
339 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

618 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