Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7043 > unrolled thread

call member function without object-compile error

Started byeric <cneric12lin0@gmail.com>
First post2011-06-21 20:01 -0700
Last post2011-06-21 21:54 -0700
Articles 7 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  call member function without object-compile error eric <cneric12lin0@gmail.com> - 2011-06-21 20:01 -0700
    Re: call member function without object-compile error Ian Collins <ian-news@hotmail.com> - 2011-06-22 15:12 +1200
      Re: call member function without object-compile error eric <cneric12lin0@gmail.com> - 2011-06-21 20:28 -0700
        Re: call member function without object-compile error Ian Collins <ian-news@hotmail.com> - 2011-06-22 15:55 +1200
          Re: call member function without object-compile error eric <cneric12lin0@gmail.com> - 2011-06-21 21:36 -0700
            Re: call member function without object-compile error Ian Collins <ian-news@hotmail.com> - 2011-06-22 16:43 +1200
              Re: call member function without object-compile error eric <cneric12lin0@gmail.com> - 2011-06-21 21:54 -0700

#7043 — call member function without object-compile error

Fromeric <cneric12lin0@gmail.com>
Date2011-06-21 20:01 -0700
Subjectcall member function without object-compile error
Message-ID<26f1f45a-7441-411a-942d-d4e70b853922@j14g2000prn.googlegroups.com>
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 just like to know how to fix (88:30) and (96:30)
thanks a lot in advance, Eric

[toc] | [next] | [standalone]


#7044

FromIan Collins <ian-news@hotmail.com>
Date2011-06-22 15:12 +1200
Message-ID<96d4tjFmtvU4@mid.individual.net>
In reply to#7043
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 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:

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

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#7045

Fromeric <cneric12lin0@gmail.com>
Date2011-06-21 20:28 -0700
Message-ID<39a31ad8-bfa6-4cf7-a9e0-64192fdb9cdb@h12g2000pro.googlegroups.com>
In reply to#7044
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 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:
>
> <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 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.
>
> --
> 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

[toc] | [prev] | [next] | [standalone]


#7046

FromIan Collins <ian-news@hotmail.com>
Date2011-06-22 15:55 +1200
Message-ID<96d7euFmtvU5@mid.individual.net>
In reply to#7045
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 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:
>>
>> <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 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

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

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#7048

Fromeric <cneric12lin0@gmail.com>
Date2011-06-21 21:36 -0700
Message-ID<42f86cc8-4dd9-4f22-af17-6751e176485d@r27g2000prr.googlegroups.com>
In reply to#7046
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 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:
>
> >> <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 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
>
> 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

[toc] | [prev] | [next] | [standalone]


#7049

FromIan Collins <ian-news@hotmail.com>
Date2011-06-22 16:43 +1200
Message-ID<96da80FmtvU6@mid.individual.net>
In reply to#7048
On 06/22/11 04:36 PM, eric wrote:
> -------------------------
> 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

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

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#7050

Fromeric <cneric12lin0@gmail.com>
Date2011-06-21 21:54 -0700
Message-ID<8d3940ca-554b-42e5-aa87-33ddcec4310c@x38g2000pri.googlegroups.com>
In reply to#7049
On Jun 21, 9:43 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 06/22/11 04:36 PM, eric wrote:
>
>
>
> > -------------------------
> > 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
>
> Your base class default constructor and virtual method don't have bodies.
>
> --
> Ian Collins

----------------------
Dear Ian:
  Thanks your tightly effort and fast response, now it compile. Eric

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web