-1

I've applied all the solutions provided by you for similar problems but I'm still getting this error:

 undefined reference to `setgolf(golf&, char*, int)'
collect2: error: ld returned 1 exit status

here is the code header

#ifndef SANS_H_INCLUDED
#define SANS_H_INCLUDED
const int len = 40;
struct golf 
{
    char fullname[len];
    int handicap;
};

//void setgolf(golf &,char * ,int);
int setgolf(golf &);
void sethandicap(golf &,int );
void showgolf(const golf &);    
#endif

here is the file containing the functions definitions

#include <iostream>
#include <cstring>
#include "chapter9exe3.h"
void setgolf(golf & s,char *c ,int hc)    
{    
    strcpy(s.fullname,c);
    s.handicap=hc;
}

int setgolf(golf & s)    
{    
    std::cin.getline(s.fullname,len);
    if(s.fullname[0] == '\0'&& s.fullname[1] == '\0' && s.fullname[2] == '\0')    
    return 0;   
    else
    return 1;
}

void sethandicap(golf & s,int n )
{
    s.handicap = n;    
}

void showgolf(const golf & s)
{
    std::cout<<"the full name :"<<s.fullname<<std::endl;
    std::cout<<"the handcape  :"<<s.handicap<<std::endl;    
}

here is the file containing the main function

#include <iostream>
#include "sans.h"
int main()
{    
    golf player;
    char name[len] = "santers God's men";
    int handicap = 84;
    setgolf(player,name,handicap);
    return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
giles
  • 85
  • 1
  • 9
  • You declared in your header the function `int setgolf(golf &);` but you've then defined 2 versions with 1 param and 3 params so there is firstly a mismatch plus the 3 param version is commented out – EdChum Nov 24 '17 at 11:39
  • I'm voting to close this as off-topic due to being caused be a simply typographical error: leaving essential code commented-out. – underscore_d Nov 24 '17 at 11:59

1 Answers1

2

Uncomment this:

//void setgolf(golf &,char * ,int);

in your header file, since you are doing in main:

setgolf(player,name,handicap);

which will search for a matching prototype in the header file, and will fail to fidn it, because you have commented it.

In other words, you call the function with three parameters, but only provide a prototype of this function in the header file with one parameter. As a result, the reference to setgolf() with three parameters is definetely undefined!

gsamaras
  • 71,951
  • 46
  • 188
  • 305