ambiguous overload
源代码 :程序代码:
#include"Query.hpp" #include"Textquery.hpp" #include<vector> #include<iostream> #include<map> #include<set> #include<fstream> #include<sstream> #include<iterator> #include<string> using namespace std; string make_plural(size_t k,const string &s1,const string &s2){ return (k==1)?s1:s1+s2; } void print_results(const set<Textquery::lines>&locs,const Textquery &file){ typedef set<Textquery::lines>linenums; linenums::size_type size=locs.size(); cout<<"match occurs"<<size<<" "<<make_plural(size,"time","s")<<endl; linenums::const_iterator it=locs.begin(); for(;it!=locs.end();++it){ cout<<"\t(line"<<(*it)+1<<")" <<file.textline(*it)<<endl; } } int main(){ cout<<"what ou want to open to query>"<<endl; string s; cin>>s; ifstream infile; infile.open(s.c_str()); Textquery tq; tq.readfile(infile); if(infile){ Query q=Query("to") & Query("of") | Query("a"); const set<Textquery::lines>locs=q.eval(tq); cout<<q<<endl; print_results(locs,tq); return 0; } }
query 类:
程序代码:
#ifndef QUERY #define QUERY #include<algorithm> #include"Textquery.hpp" #include<set> #include<string> #include<fstream> #include<iostream> using namespace std; class Querybase{ friend class Query; protected: typedef Textquery::lines lines; virtual ~Querybase(){} private: virtual std::set<lines> eval(const Textquery&)const=0; virtual std::ostream& display(std::ostream& = std::cout)const=0; }; class Query{ friend Query operator~(const Query&); friend Query operator&(const Query&,const Query &); friend Query operator|(const Query &,const Query &); public: Query(const std::string&); Query(const Query&c):q(c.q),use(c.use){ ++*use; } ~Query(){ decr_use(); } Query& operator=(const Query&); std::set<Textquery::lines>eval(const Textquery&t)const{ return q->eval(t); } std::ostream&display(std::ostream &os)const{ return q->display(os); } Query(Querybase *query):q(query),use(new std::size_t(1)){ } private: Querybase *q; std::size_t*use; void decr_use() { if(--*use==0){ delete q; delete use; } } }; inline Query& Query::operator=(const Query &n){ ++*n.use; decr_use(); q=n.q; use=n.use; return *this; } inline ostream& operator<<(ostream &os,const Query &q) { return q.display(os); } class WordQuery:public Querybase{ friend class Query; WordQuery(const std::string&s):queryword(s){} std::set<lines>eval(const Textquery &t)const{ return t.runquery(queryword); } std::ostream &display(std::ostream&os)const{ return os<<queryword; } std::string queryword; }; inline Query::Query(const string &s): q(new WordQuery(s)),use(new size_t(1)){ } class NotQuery:public Querybase{ friend Query operator~(const Query&); NotQuery(Query q):query(q){} std::set<lines>eval(const Textquery&)const; std::ostream& display(std::ostream &os)const{ return os<<"~("<<query<<")"; } const Query query; }; class BinaryQuery:public Querybase{ protected: BinaryQuery(Query left,Query right,std::string op): lhs(left),rhs(right),oper(op){} std::ostream& display(std::ostream &os)const{ return os<<"("<<lhs<<" "<<oper<<" "<<rhs<<")"; } const Query lhs,rhs; const std::string oper; }; class AndQuery:public BinaryQuery{ public: friend Query operator&(const Query&,const Query&); AndQuery(Query left,Query right): BinaryQuery(left,right,"&"){} std::set<lines>eval(const Textquery&)const; }; class OrQuery:public BinaryQuery{ public: friend Query operator|(const Query&,const Query&); OrQuery(Query left,Query right): BinaryQuery(left,right,"|"){} std::set<lines>eval(const Textquery&)const; }; inline Query operator~(const Query &oper) { return new NotQuery(oper); } inline Query operator&(const Query &lhs,const Query rhs){ return new AndQuery(lhs,rhs); } inline Query operator|(const Query &lhs,const Query rhs){ return new OrQuery(lhs,rhs); } set<Textquery::lines>OrQuery::eval(const Textquery&file)const{ set<lines>right=rhs.eval(file), ret_lines=lhs.eval(file); ret_lines.insert(right.begin(),right.end()); return ret_lines; } set<Textquery::lines>AndQuery::eval(const Textquery&file)const{ set<lines>left=lhs.eval(file), right=rhs.eval(file); set<lines>ret_lines; set_intersection(left.begin(),left.end(),right.begin(),right.end(), inserter(ret_lines,ret_lines.begin())); return ret_lines; } set<Textquery::lines>NotQuery::eval(const Textquery &file)const{ set<Textquery::lines>has_val=query.eval(file); set<lines>ret_lines; for(Textquery::lines n=0;n!=file.size();++n) { if((has_val.find(n)==has_val.end())) ret_lines.insert(n); } return ret_lines; } #endif
我用cfree编译之后出现了这样一个错误
[Error] C:\Documents and Settings\Administrator\桌面\新建文件夹\15.42.cpp:37: error: ambiguous overload for 'operator&' in 'Query(((const std::string&)(&string(((const char*)"to"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>()))))))) & Query(((const std::string&)(&string(((const char*)"of"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>())))))))'
新手求解,