您的位置:首页 > 编程语言 > C语言/C++

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

2014-02-19 16:53 239 查看
单独编译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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐