Dear gcc programers: I tried to copy (from internet download book's example source code, from http://comscigate.com/BookCode/cppbooks.htm on C++ Primer 3rd Ed, written by Lippman , Lajoie that source code is written before 1999 and I guess is compiled based on borland's compiler I tried to compile/run chap17, text_query.C attach 4 related files(I already modify a little bit, ie replace iostream.h by iostream and add using namespace std;) ------------------ eric@eric-laptop:~/CppPrimer3/download/chap17$ g++ Query.C text_query.C In file included from Query.C:1:0: Query.h:27:41: error: type/value mismatch at argument 3 in template parameter list for ‘template<class _Key, class _Compare, class _Alloc> class std::set’ Query.h:27:41: error: expected a type, got ‘allocator’ Query.h:28:36: error: type/value mismatch at argument 2 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’ Query.h:28:36: error: expected a type, got ‘allocator’ ---------- this is just first 4 errors of the whole(a lot). /* which I believe is gcc specific(4.5.2), so I decide not post to comp.lang.c++ */ plz help, and thanks a lot in advance, Eric
#include "TextQuery.h" int main() { TextQuery tq; tq.build_up_text(); tq.query_text(); return 0; }
#include <algorithm> #include <string> #include <vector> #include <utility> #include <map> #include <set> // #include <iostream.h> #include <iostream> // #include <fstream.h> #include <fstream> #include <stddef.h> #include <ctype.h> using namespace std; typedef pair<short,short> location; typedef vector<location,allocator> loc; typedef vector<string,allocator> text; typedef pair<text*,loc*> text_loc; #include "Query.h" #include "UserQuery.h" class TextQuery { public: TextQuery() { memset( this, 0, sizeof( TextQuery )); } static void filter_elements( string felems ) { filt_elems = felems; } void query_text(); void display_map_text(); void display_text_locations(); void build_up_text() { retrieve_text(); separate_words(); filter_text(); suffix_text(); strip_caps(); build_word_map(); } private: void retrieve_text(); void separate_words(); void filter_text(); void strip_caps(); void suffix_text(); void suffix_s( string& ); void build_word_map(); void init_query_statics(); void display_solution(); private: vector<string,allocator> *lines_of_text; text_loc *text_locations; map<string,loc*,less<string>,allocator> *word_map; Query *query; static string filt_elems; vector<int,allocator> line_cnt; }; inline void TextQuery:: init_query_statics() { NotQuery::all_locs( text_locations->second ); AndQuery::max_col( &line_cnt ); UserQuery::word_map( word_map ); }
#ifndef QUERY_H_ #define QUERY_H_ #include <vector> // #include <iostream.h> #include <iostream> #include <set> #include <string> #include <utility> using namespace std; typedef pair< short, short > location; class Query { public: virtual ~Query(){ delete _solution; } virtual void eval() = 0; virtual Query *clone() const = 0; virtual ostream& print( ostream &os ) const = 0; virtual bool add_op( Query* ) { return true; } virtual void display_solution( ostream &os = cout, int tabcnt = 0 ); Query& operator=( const Query& ); void handle_tab( ostream &os, int tabcnt ) const; const set< short,less<short>,allocator > *solution(); const vector< location, allocator > *locations() const { return &_loc; } void lparen( short lp ) { _lparen = lp; } void rparen( short rp ) { _rparen = rp; } short lparen() { return _lparen; } short rparen() { return _rparen; } void print_lparen( short cnt, ostream& os ) const; void print_rparen( short cnt, ostream& os ) const; void display_partial_solution(); protected: // copy constructor and copy assignment operator? Query() : _lparen( 0 ), _rparen( 0 ), _solution( 0 ) {} Query( const Query &rhs ) : _loc( rhs._loc ), _solution( 0 ), _lparen( rhs._lparen ), _rparen( rhs._rparen ) {} Query( const vector<location, allocator> &loc ) : _loc( loc ), _solution( 0 ), _lparen( 0 ), _rparen( 0 ) {} set<short,less<short>,allocator>* _vec2set( const vector<location,allocator>* ); short _lparen; short _rparen; vector<location,allocator> _loc; set<short,less<short>,allocator> *_solution; }; inline ostream& operator<<( ostream &os, const Query &q ) { return q.print( os ); } class NameQuery : public Query { public: NameQuery( string name ) : _name( name ) {} NameQuery( string name, const vector<location, allocator> &loc ) : Query( loc ), _name( name ) {} virtual void eval(); virtual ostream& print( ostream &os ) const; virtual void display_solution( ostream &os = cout, int tabcnt = 0 ); virtual Query *clone() const { return new NameQuery( *this ); } string name() const { return _name; } protected: string _name; }; class NotQuery : public Query { public: NotQuery( Query *op = 0 ) : _op( op ) {} NotQuery( const NotQuery& ); ~NotQuery() { delete _op; } virtual void eval(); virtual ostream& print( ostream &os ) const; virtual bool add_op( Query* ); virtual void display_solution( ostream &os = cout, int tabcnt = 0 ); virtual Query *clone() const; NotQuery &operator=( const NotQuery& ); const Query *op() const { return _op; } static void all_locs( const vector< location, allocator > *ploc ) { if ( !_all_locs ) _all_locs = ploc; } protected: Query *_op; static const vector< location, allocator > *_all_locs; }; class OrQuery : public Query { public: OrQuery( Query *lop = 0, Query *rop = 0 ) : _lop( lop ), _rop( rop ) {} OrQuery( const OrQuery& ); ~OrQuery() { delete _lop; delete _rop; } OrQuery& operator=( const OrQuery& ); virtual void eval(); virtual ostream& print( ostream &os ) const; virtual bool add_op( Query* ); virtual void display_solution( ostream &os=cout, int tabcnt=0 ); virtual Query* clone() const; const Query *rop() const { return _rop; } const Query *lop() const { return _lop; } protected: Query *_lop; Query *_rop; }; class AndQuery : public Query { public: AndQuery( Query *lop = 0, Query *rop = 0 ) : _lop( lop ), _rop( rop ) {} ~AndQuery() { delete _lop; delete _rop; } AndQuery( const AndQuery& ); AndQuery& operator=( const AndQuery& ); virtual void eval(); virtual ostream& print( ostream &os ) const; virtual bool add_op( Query* ); virtual void display_solution( ostream &os=cout, int tabcnt=0 ); virtual Query* clone() const; const Query *rop() const { return _rop; } const Query *lop() const { return _lop; } static void max_col( const vector< int, allocator > *pcol ) { if ( !_max_col ) _max_col = pcol; } protected: Query *_lop; Query *_rop; static const vector< int, allocator > *_max_col; }; class less_than_pair { public: bool operator()( location loc1, location loc2 ) { return (( loc1.first < loc2.first ) || ( loc1.first == loc2.first ) && ( loc1.second < loc2.second )); } }; inline Query& Query:: operator=( const Query &rhs ) { if ( &rhs != this ) { _lparen = rhs._lparen; _rparen = rhs._rparen; _loc = rhs._loc; delete _solution; _solution = 0; } return *this; }; inline const set< short, less<short>,allocator >* Query:: solution() { if ( !_solution ) _solution = _vec2set( &_loc ); return _solution; } inline void Query:: display_partial_solution() { cout << "\t" << *this << " ( " << solution()->size() << " ) lines match" << endl; } inline NotQuery& NotQuery:: operator=( const NotQuery &rhs ) { if ( &rhs != this ) { this->Query::operator=( rhs ); // delete _op; _op = rhs._op->clone(); } return *this; }; inline NotQuery:: NotQuery( const NotQuery &rhs ) : Query( rhs ) { _op = rhs._op->clone(); }; inline AndQuery& AndQuery:: operator=( const AndQuery &rhs ) { if ( &rhs != this ) { this->Query::operator=( rhs ); _lop = rhs._lop->clone(); _rop = rhs._rop->clone(); } return *this; }; inline AndQuery:: AndQuery( const AndQuery &rhs ) : Query( rhs ) { _lop = rhs._lop->clone(); _rop = rhs._rop->clone(); }; inline OrQuery& OrQuery:: operator=( const OrQuery &rhs ) { if ( &rhs != this ) { this->Query::operator=( rhs ); _lop = rhs._lop->clone(); _rop = rhs._rop->clone(); } return *this; }; inline OrQuery:: OrQuery( const OrQuery &rhs ) : Query( rhs ) { _lop = rhs._lop->clone(); _rop = rhs._rop->clone(); }; inline bool AndQuery:: add_op( Query *op ) { bool status = false; if ( !_lop ) { _lop = op; status = true; } else if ( ! _rop ) { _rop = op; status = true; } return status; } inline bool OrQuery:: add_op( Query *op ) { bool status = false; if ( !_lop ) { _lop = op; status = true; } else if ( ! _rop ) { _rop = op; status = true; } return status; } inline bool NotQuery:: add_op( Query *op ) { bool status = false; if ( !_op ) { _op = op; status = true; } return status; } inline void AndQuery:: display_solution( ostream &os, int tabcnt ) { handle_tab( os, tabcnt ); os << "&& solution line set: "; Query::display_solution( os, 0 ); _lop->display_solution( os, tabcnt+1 ); _rop->display_solution( os, tabcnt+1 ); } inline void OrQuery:: display_solution( ostream &os, int tabcnt ) { handle_tab( os, tabcnt ); os << "|| solution line set: "; Query::display_solution( os, 0 ); _lop->display_solution( os, tabcnt+1 ); _rop->display_solution( os, tabcnt+1 ); } inline void NotQuery:: display_solution( ostream &os, int tabcnt ) { handle_tab( os, tabcnt ); os << "! solution line set: "; Query::display_solution( os, 0 ); _op->display_solution( os, tabcnt+1 ); } inline void NameQuery:: display_solution( ostream &os, int tabcnt ) { handle_tab( os, tabcnt ); os << _name << " solution line set: "; Query::display_solution( os, 0 ); } inline ostream& AndQuery:: print( ostream &os ) const { if ( _lparen ) print_lparen( _lparen, os ); _lop->print( os ); os << " && "; _rop->print( os ); if ( _rparen ) print_rparen( _rparen, os ); return os; } inline ostream& OrQuery:: print( ostream &os ) const { if ( _lparen ) print_lparen( _lparen, os ); _lop->print( os ); os << " || "; _rop->print( os ); if ( _rparen ) print_rparen( _rparen, os ); return os; } inline ostream& NotQuery:: print( ostream &os ) const { os << " ! "; if ( _lparen ) print_lparen( _lparen, os ); _op->print( os ); if ( _rparen ) print_rparen( _rparen, os ); return os; } inline ostream& NameQuery:: print( ostream &os ) const { if ( _lparen ) print_lparen( _lparen, os ); os << _name; if ( _rparen ) print_rparen( _rparen, os ); return os; } #endif
#include "Query.h" using namespace std; const vector< location,allocator > *NotQuery::_all_locs = 0; const vector< int,allocator > *AndQuery::_max_col = 0; void display_location( const vector<location,allocator>*ploc ) { cout << "\ndisplay_location vector: \n"; vector<location, allocator>::const_iterator iter = ploc->begin(), iter_end = ploc->end(); for ( ; iter != iter_end; ++iter ) { cout << "\tfirst: " << ( *iter ).first; cout << "\tsecond: " << ( *iter ).second << endl; } } set<short,less<short>,allocator>* Query:: _vec2set( const vector<location,allocator> *pvec ) { set<short,less<short>,allocator> *ps = new set<short,less<short>,allocator>; vector<location,allocator>::const_iterator iter = pvec->begin(), iter_end = pvec->end(); for ( ; iter != iter_end; ++iter ) ps->insert(( *iter ).first ); return ps; } void AndQuery:: eval() { // add an exception here if ( ! _lop || ! _rop ) { cerr << "Internal error: AndQuery: \n" << "no " << ( _lop ? "right " : "left " ) << "operand -- bailing out... \n"; return; } _lop->eval(); _rop->eval(); vector< location, allocator >::const_iterator riter = _rop->locations()->begin(), liter = _lop->locations()->begin(), riter_end = _rop->locations()->end(), liter_end = _lop->locations()->end(); while ( liter != liter_end && riter != riter_end ) { // while left line number is greater than right while ( (*liter).first > (*riter).first ) { ++riter; if ( riter == riter_end ) { display_partial_solution(); display_location( &_loc ); return; } } // while left line number is less than right while ( (*liter).first < (*riter).first ) { if ( ((*liter).first == (*riter).first-1 ) && ((*riter).second == 0 ) && ((*liter).second == (*_max_col)[ (*liter).first ] )) { _loc.push_back( *liter ); _loc.push_back( *riter ); ++riter; if ( riter == riter_end ) { display_partial_solution(); display_location( &_loc ); return; } } ++liter; if ( liter == liter_end ) { display_partial_solution(); display_location( &_loc ); return; } } // while both are on the same line while ( (*liter).first == (*riter).first ) { if ( (*liter).second+1 == ((*riter).second) ) { _loc.push_back( *liter ); _loc.push_back( *riter ); ++riter; ++liter; } else if ( (*liter).second <= (*riter).second ) ++liter; else ++riter; if ( liter == liter_end || riter == riter_end ) { display_partial_solution(); display_location( &_loc ); return; } } } } void OrQuery:: eval() { // add an exception here if ( ! _lop || ! _rop ) { cerr << "Internal error: OrQuery: \n" << "no " << ( _lop ? "right " : "left " ) << "operand -- bailing out... \n"; return; } _lop->eval(); _rop->eval(); vector< location, allocator >::const_iterator riter = _rop->locations()->begin(), liter = _lop->locations()->begin(), riter_end = _rop->locations()->end(), liter_end = _lop->locations()->end(); merge( liter, liter_end, riter, riter_end, inserter( _loc, _loc.begin() ), less_than_pair() ); display_partial_solution(); display_location( &_loc ); } void NotQuery:: eval() { // add an exception here if ( ! _op ) { cerr << "Internal error: NotQuery: \n" << "no operand -- bailing out... \n"; return; } _op->eval(); vector< location, allocator >::const_iterator iter = _all_locs->begin(), iter_end = _all_locs->end(); set<short,less<short>,allocator> *ps = _vec2set( _op->locations() ); for ( ; iter != iter_end; ++iter ) { if ( ! ps->count( (*iter).first )) { _loc.push_back( *iter ); } } _solution = _vec2set( &_loc ); display_partial_solution(); display_location( &_loc ); } void NameQuery:: eval() { display_partial_solution(); display_location( &_loc ); } void Query:: handle_tab( ostream &os, int tabcnt ) const { while ( tabcnt-- ) os << '\t'; } void Query:: display_solution( ostream &os, int tabcnt ) { for ( int tcnt = tabcnt; tcnt; --tcnt ) os << '\t'; const set< short, less<short>, allocator > *pset = solution(); os << "( " << pset->size() << " ) : "; set< short, less<short>, allocator >::iterator iter = pset->begin(); for ( int elem_cnt = 1; iter != pset->end(); ++iter, ++elem_cnt ) { if ( !( elem_cnt % 10 )) { os << endl; elem_cnt = 1; for ( int tcnt = tabcnt; tcnt; --tcnt ) os << '\t'; } os << *iter+1 << ' '; } os << endl; } Query* NotQuery:: clone() const { return new NotQuery( *this ); } Query* AndQuery:: clone() const { return new AndQuery( *this ); } Query* OrQuery:: clone() const { return new OrQuery( *this ); } void Query:: print_lparen( short cnt, ostream &os ) const { while ( cnt-- != 0 ) os << "("; os << ' '; } void Query:: print_rparen( short cnt, ostream &os ) const { os << ' '; while ( cnt-- != 0 ) os << ")"; }