call member function without object-compile error

E

eric

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 ‘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

Ian Collins

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:

-----------------------------
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$

For the protected access, provide a public member function to return the
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.
 
E

eric

For the protected access, provide a public member function to return the
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
 
I

Ian Collins

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?
 
E

eric

What about the other two suggestion to fix your actual problem?

------------------------------------------------------------------------
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$
 
I

Ian Collins

-------------------------
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$

Your base class default constructor and virtual method don't have bodies.
 
E

eric

-------------------------
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$

Your base class default constructor and virtual method don't have bodies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top