Reputation: 5931
It shows " Linker error: fatal error LNK1120: 1 unresolved externals " Whole project = header file, 1st .cpp file and 2nd .cpp file with main() function. Any1 has idea what ive done wrong?
//golf.h for golf.cpp
const int Len = 40;
struct golf{
char fullname[Len];
int handicap;
};
//dla gotowych wartosci
void setgolf (golf & g, const char * name, int hc);
//do wpisywania wartosci
int setgolf (golf & g);
//tylko handicap
inline void handicap (golf & g, int hc);
//wyswietla info o graczu
void showgolf (const golf & g) ;
NEXT FILE
//golf.cpp for 9-1.cpp
#include <iostream>
#include <cstring>
#include "golf.h"
extern const int Len;
void setgolf (golf & g, const char* name, int hc){
strcpy(g.fullname,name);
g.handicap=hc;
}
int setgolf (golf & g){
using namespace std;
cout << "Podaj fullname golfiarza" << endl;
if (cin.getline(g.fullname,Len)) return 0;
cout << "Teraz podaj jego handicap" << endl;
cin >> g.handicap;
return 1;
}
inline void handicap (golf & g, int hc){
g.handicap=hc;
}
void showgolf (const golf & g){
using std::cout;
using std::endl;
cout << "Fullname gracza to: " << g.fullname << endl;
cout << "Jego handicap to: " << g.handicap << endl;
}
LAST FILE
#include <iostream>
#include <cstdlib>
#include "golf.h"
using namespace std;
int main(){
cout << "Witaj!\nTutaj program golficzny!" << endl;
golf filip;
golf klaudia;
cout << "Automatyczne uzupelnienie Filipa" << endl;
setgolf(filip, "Filip Bartuzi",100);
showgolf(filip);
cout << "Manualne uzupelnienie Klaudii" << endl;
((setgolf(klaudia))==1) ? showgolf(klaudia) : cout << "nie wprowadziles gracza!" << endl; ;
cout << "Zly handicap? Okey, zmienie handicap Filipowi" << endl;
handicap(filip,50);
showgolf(filip);
cout << "Od razu lepiej, nieprawda?" << endl;
system("PAUSE");
return 0;
}
Any idea?
Upvotes: 1
Views: 4368
Reputation: 20393
inline void handicap (golf & g, int hc){
g.handicap=hc;
}
Try removing the inline
keyword if the function is in golf.cpp
file.
Alternatively, relocate the entire function in the golf.h
file. (Here, this choice seems more appropriate.)
Reason: If a function is to be inline
, then its body must be visible to the callers. That is possible only if the function body is placed in the header file, not the implementation file.
Possibly relevant: C++ inline member function in .cpp file
Upvotes: 4
Reputation: 12907
Remove the inline word. It's meant to provide both a declaration and a definition of the function at the same place (which make the compiler possibly replace every call to the function by its code).
In your code, you use the inline keyword, but provide the code somewhere else.
It should look like this :
inline void handicap (golf & g, int hc){ g.handicap=hc; }
directly in your .h file. (And the definition can be removed from the golf.cpp file)
Upvotes: 2