| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1825 人关注过本帖
标题:ambiguous overload
只看楼主 加入收藏
未未来
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:182
专家分:157
注 册:2012-11-6
结帖率:94.87%
收藏
已结贴  问题点数:2 回复次数:2 
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>())))))))'


新手求解,
搜索更多相关主题的帖子: 源代码 
2013-05-27 23:47
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:2 
你先解释一下这个编译错误是什么意思?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-05-28 07:06
未未来
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:182
专家分:157
注 册:2012-11-6
收藏
得分:0 
回复 2楼 peach5460
重载函数模糊,出现两义性?
2013-05-28 11:14
快速回复:ambiguous overload
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019531 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved