首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新手 疑问解决思路

2012-09-20 
新手 疑问#includevector #includefstream #includestring#includeset#includemap class TextQu

新手 疑问
#include<vector>
#include<fstream>
#include<string>
#include<set>
#include<map>
class TextQuery{
public:
typedef std::string::size_type str_size;
typedef std::vector<std::string>::size_type line_no;
void read_file(std::ifstream &is){
store_file(is);
build_map();
}
std::set<line_no> run_query(const std::string&) const;
std::string text_line(line_no) const;
line_no size() const;
private:
void store_file(std::ifstream&);
void build_map();
std::vector<std::string> lines_of_text;
std::map< std::string,std::set<line_no> > word_map;
static std::string cleanup_str(const std::string&);
};

这些是c++ primer上的15.9文本查询程序的代码 编译后出错
[Error] D:\TextQuery+\TextQuery\TextQuery.h:7: error: redefinition of `class TextQuery'
[Error] D:\TextQuery+\TextQuery\TextQuery.h:7: error: previous definition of `class TextQuery'
为什么会出现重复定义的错呢??

加上#ifndef TEXTQUERY_H
  #define TEXTQUERY_H
....
  #endif 就可以通过编译 为什么呀?

小弟新手 不胜感激~~~~

[解决办法]
防止重复引用
[解决办法]
同新手,等答案....

[解决办法]
你的TextQuery.h被重复引用了一次以上,保护直接和间接的引用,尤其是间接的引用不那么直观。加上#ifndef等语句,第一次引用时,定义了TEXTQUERY_H,以后再引用的时候实际上不会再把内容include进来了,这样确保只引用到一次。

热点排行