Re: call member function without object-compile error
On Jun 21, 8:55 pm, Ian Collins <ian-n...@hotmail.com> wrote:
On 06/22/11 03:28 PM, eric wrote:
On Jun 21, 8:12 pm, Ian Collins<ian-n...@hotmail.com> wrote:
On 06/22/11 03:01 PM, eric wrote:
Dear c++ advancer:
I have a simple c++ file, which is I copy and try to test fro=
m 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:
<snip for brevity>
-----------------------------
the following is my g++ result(4.5.2)
--------------------
eric@eric-laptop:~/CppPrimer3$ g++ p904.cpp
p904.cpp: In member function bool NameQuery::compare0(const Query*) :
p904.cpp:36:24: error: std::vector<std::pair<short int, short int> =
>
Query::_loc is protected
p904.cpp:88:30: error: within this context
p904.cpp: In function int main() :
p904.cpp:96:30: error: cannot call member function bool
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
For the protected access, provide a public member function to return t=
he
vector size.
For the call, you have to have an instance of NameQuery to call a
non-static member function.
As a general point, names starting with leading underscores may clash
with names used by your implementation.
Thanks your reply and suggestion
but after I change leading underscore to a_ (add an "a" in front of
all _variablename)
my g++ still response name error : Cann't call memeber function
without object on (88:30 and 96:30) /* try by yourself */
so
still need any advancer's help and suggestion and thanks a lot in
advance, Eric
What about the other two suggestion to fix your actual problem?
--
Ian Collins
------------------------------------------------------------------------
this is newest file I fix/modify with your suggestion
---------------------------------------
#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
Query();
// 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 &a_loc; }
int f_b() { return a_loc.size(); }
protected:
//Query();
set<short> *a_vec2set( const vector<location>* );
static vector<string> *a_text_file;
set<short> *a_solution;
vector<location> a_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 )
: a_solution( 0 ), a_loc( loc )
{}
class NameQuery : public Query {
public:
// ...
// overrides virtual Query::eval() instance
void eval();
// read access function
string name() const { return a_name; }
static const map<string,loc*> *word_map() { return a_word_map; }
bool compare0( Query *pquery);
protected:
string a_name;
static map<string,loc*> *a_word_map;
};
bool
NameQuery::
compare0( Query *pquery )
{
// ok: protected member of its Query subobject
int myMatches = a_loc.size();
// error: has no direct access rights to the
// protected member of an independent query object
int itsMatches = pquery->f_b();
return myMatches == itsMatches;
}
int main() {
bool aa;
Query *p1;
NameQuery d;
aa = d.compare0(p1);
return 0;
}
-------------------------
my g++ reponse linker error
--------------
eric@eric-laptop:~/CppPrimer3$ g++ p904.cpp
/tmp/ccBLCYgi.o: In function `NameQuery::NameQuery()':
p904.cpp:(.text._ZN9NameQueryC2Ev[_ZN9NameQueryC5Ev]+0xe): undefined
reference to `Query::Query()'
p904.cpp:(.text._ZN9NameQueryC2Ev[_ZN9NameQueryC5Ev]+0x17): undefined
reference to `vtable for NameQuery'
/tmp/ccBLCYgi.o: In function `NameQuery::~NameQuery()':
p904.cpp:(.text._ZN9NameQueryD2Ev[_ZN9NameQueryD5Ev]+0xc): undefined
reference to `vtable for NameQuery'
collect2: ld returned 1 exit status
eric@eric-laptop:~/CppPrimer3$
----------------------------------
Need any advancer's suggestion and help again, and thanks a lot in
advance, Eric