C++ primer中,"单词转换"map对象源码问题

单独编译word_transform.cc文件的时候报错:

word_transform.cc:(.text+0x133):对‘open_file(std::basic_ifstream<char, std::char_traits<char> >&, std::string const&)’未定义的引用
word_transform.cc:(.text+0x533):对‘open_file(std::basic_ifstream<char, std::char_traits<char> >&, std::string const&)’未定义的引用
word_transform.cc:(.text+0x703):对‘open_file(std::basic_ifstream<char, std::char_traits<char> >&, std::string const&)’未定义的引用
collect2: 错误: ld 返回 1

原因:word_transform.cc这个文件的源码中,只在最上面声明了open_file()函数。但是没有在后面实现该函数,所以,会报open_file()未定义的错误。

ifstream& open_file(ifstream&, const string&); 

其源码如下: 
ifstream& open_file(ifstream &in, const string &file) 
{ 
in.close(); // close in case it was already open 
in.clear(); // clear any existing errors 
// if the open fails, the stream will be in an invalid state 
in.open(file.c_str()); // open the file we were given 
return in; // condition state is good if open succeeded 
} 
在word_transform.cc文件最下面加上open_file()函数源码就可以正常运行了。

注:通过makefile文件将gnu_files文件夹下所有源码一起编译word_transform.cc是可以通过的,所以,说明open_file这个函数是在某个文件里面实现了。只是本来找人半天没找到在哪里。如果有人找到可以tell me,tks吐舌头

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值