在编译多文件时出现了链接错误,代码如下
/*c++pp_9_1.h---第一个文件*/
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//non-interactive version:
//function sets golf structure to provided name,handicp
//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 handicp to new value
void handicap(golf & g,int hc);
//function displays contents of golf structure
void showgolf(const golf & g);
/*c++pp_9_1.cpp ---第二个文件*/
#include <iostream>
#include <cstring>
#include "c++pp_9_1.h"
void setgolf(golf & g,const char * name,int hc)
{
strcpy(g.fullname,name);
handicap(g,hc);
}
int setgolf(golf & g)
{
using namespace std;
char name[Len];
int hc;
cout << "Enter a name: ";
cin.getline(name,Len);
if(name[0] == '\n')
return 0;
cout << "Enter the handicp: ";
cin >> hc;
setgolf(g,name,hc);
return 1;
}
void handicap(golf & g,int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "The name: " << g.fullname << std::endl;
std::cout << "The handicap: " << g.handicap << std::endl;
}
/*c++pp_9_1_main.cpp---第三个文件*/
#include <iostream>
#include "c++pp_9_1.h"
int main(void)
{
using namespace std;
golf g;
while(setgolf(g) == 1)
{
showgolf(g);
cout << "Next or Enter empty line to quit" << endl;
}
return 0;
}
编译时出现了错误:>c++pp_9_1_main.obj : error LNK2019: 无法解析的外部符号 "int __cdecl setgolf(struct golf &)" (?setgolf@@YAHAAUgolf@@@Z),该符号在函数 _main 中被引用
1>c++pp_9_1_main.obj : error LNK2019: 无法解析的外部符号 "void __cdecl showgolf(struct golf const &)" (?showgolf@@YAXABUgolf@@@Z),该符号在函数 _main 中被引用
1>E:\VS2012temp\test\Debug\test.exe : fatal error LNK1120: 2 个无法解析的外部命令
我查看了一下,setgolf与showgolf都在头文件中声明了,并在c++pp_9_1.cpp中定义了,为什么还是会发生错误,书上的示例没错,我按照它的方法写了另一个程序却出现了错误。请各位朋友帮忙解决一下,谢谢。
/*c++pp_9_1.h---第一个文件*/
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
//non-interactive version:
//function sets golf structure to provided name,handicp
//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 handicp to new value
void handicap(golf & g,int hc);
//function displays contents of golf structure
void showgolf(const golf & g);
/*c++pp_9_1.cpp ---第二个文件*/
#include <iostream>
#include <cstring>
#include "c++pp_9_1.h"
void setgolf(golf & g,const char * name,int hc)
{
strcpy(g.fullname,name);
handicap(g,hc);
}
int setgolf(golf & g)
{
using namespace std;
char name[Len];
int hc;
cout << "Enter a name: ";
cin.getline(name,Len);
if(name[0] == '\n')
return 0;
cout << "Enter the handicp: ";
cin >> hc;
setgolf(g,name,hc);
return 1;
}
void handicap(golf & g,int hc)
{
g.handicap = hc;
}
void showgolf(const golf & g)
{
std::cout << "The name: " << g.fullname << std::endl;
std::cout << "The handicap: " << g.handicap << std::endl;
}
/*c++pp_9_1_main.cpp---第三个文件*/
#include <iostream>
#include "c++pp_9_1.h"
int main(void)
{
using namespace std;
golf g;
while(setgolf(g) == 1)
{
showgolf(g);
cout << "Next or Enter empty line to quit" << endl;
}
return 0;
}
编译时出现了错误:>c++pp_9_1_main.obj : error LNK2019: 无法解析的外部符号 "int __cdecl setgolf(struct golf &)" (?setgolf@@YAHAAUgolf@@@Z),该符号在函数 _main 中被引用
1>c++pp_9_1_main.obj : error LNK2019: 无法解析的外部符号 "void __cdecl showgolf(struct golf const &)" (?showgolf@@YAXABUgolf@@@Z),该符号在函数 _main 中被引用
1>E:\VS2012temp\test\Debug\test.exe : fatal error LNK1120: 2 个无法解析的外部命令
我查看了一下,setgolf与showgolf都在头文件中声明了,并在c++pp_9_1.cpp中定义了,为什么还是会发生错误,书上的示例没错,我按照它的方法写了另一个程序却出现了错误。请各位朋友帮忙解决一下,谢谢。


