call member function without object-compile error
Dear c++ advancer:
I have a simple c++ file, which is I copy and try to test from a
book (C++ Primer 3rd Ed, by Stanley B. Lippman and Josee Lajoie,
around page 904, chapter 17.3 Base Class Member Access)
the following is my program:
------------------------------------------
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <utility>
#include <map>
using namespace std;
typedef pair< short, short > location;
class Query {
public:
// constructors and destructor
// are discussed in Section 17.4
// copy constructor and copy assignment operator
// are discussed in Section 17.6
// operations supporting public interface
virtual void eval() = 0;
void display () const;
// read access functions
const set<short> *solution() const;
const vector<location> *locations() const { return &_loc; }
protected:
Query();
set<short> *_vec2set( const vector<location>* );
static vector<string> *_text_file;
set<short> *_solution;
vector<location> _loc;
private:
explicit Query( const vector<location>& );
};
/*
inline const set<short>*
Query::
solution()
{
return _solution
? _solution
: _solution = _vec2set( &_loc );
}
*/
typedef vector<location> loc;
inline
Query::
Query( const vector< location > &loc )
: _solution( 0 ), _loc( loc )
{}
class NameQuery : public Query {
public:
// ...
// overrides virtual Query::eval() instance
void eval();
// read access function
string name() const { return _name; }
static const map<string,loc*> *word_map() { return _word_map; }
bool compare0(const Query *);
protected:
string _name;
static map<string,loc*> *_word_map;
};
bool
NameQuery::
compare0( const Query *pquery )
{
// ok: protected member of its Query subobject
int myMatches = _loc.size();
// error: has no direct access rights to the
// protected member of an independent query object
int itsMatches = pquery->_loc.size();
return myMatches == itsMatches;
}
int main() {
bool aa;
const Query *p1;
aa = NameQuery::compare0(p1);
return 0;
}
-----------------------------
the following is my g++ result(4.5.2)
--------------------
eric@eric-laptop:~/CppPrimer3$ g++ p904.cpp
p904.cpp: In member function =91bool NameQuery::compare0(const Query*)':
p904.cpp:36:24: error: =91std::vector<std::pair<short int, short int> >
Query::_loc' is protected
p904.cpp:88:30: error: within this context
p904.cpp: In function =91int main()':
p904.cpp:96:30: error: cannot call member function =91bool
NameQuery::compare0(const Query*)' without object
eric@eric-laptop:~/CppPrimer3$
--------------------------------------
I just like to know how to fix (88:30) and (96:30)
thanks a lot in advance, Eric