The exercise 15.41 in book of c++ primer 4th [closed]
class Query{
friend Query operator&(const Query&,const Query&);
friend Query operator|(const Query&,const Query&);
friend Query operator~(const Query&);
public:
Query(const string&); //build a new WordQuery ??
Query(const Query&c):p(c.p),use(c.use){++*use;}
~Query(){delQuery();}
Query operator=(const Query&c);
set<TextQuery::line_no> eval(const TextQuery&c) const{return p->eval(c);}
ostream& display(ostream&s) const {return p->display(s);}
private:
Query(QueryBase *query):p(query),use(new size_t(1)){}
QueryBase *p;
size_t* use;
void delQuery(){
if(--*use==0)
delete p;
delete use;
}
};
class WordQuery:public QueryBase{
friend class Query;
WordQuery(const string& s):QueryWord(s){} //Query use the WordQuery constructor
set<lineno> eval(const TextQuery&t) const
{return t.run_query(QueryWord);}
ostream& display(ostream& os) const
{return os<<QueryWord;}
string QueryWord;
};
I invoke it in expression Query q=Query(s1) & Query(s2) | Query(s3);
ERRORS:"TextQueryADVANCE.cpp:(.text+0x15ba): undefined reference to `Query::Query(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
"
It appears that you put the prototype for the constructor, but you didn't actually put an implementation.
You put: Query(const string&);
, which says that there is a constructor somewhere that takes a string and creates a Query with it. However, somewhere you actually have to put something that does that.
This is a linker error. It seems you have not provided an implementation for Query(const string&)
.
精彩评论