Passing a function pointer as an argument to an overloaded >> operator.

Discussion in 'C++' started by glen stark, Sep 29, 2003.

  1. glen stark

    glen stark Guest

    Hi all.

    I'm working with an array of member function pointers (they are all get
    function of the class Bead). The typedef is:

    typedef _real (Bead::*_beadGfp)(void);

    I have a class System which contains both an array of function pointers:

    std::vector<_beadGfp> msmtFPs;

    And an array of pointers to RTSM, a class I've implemented for measurements.

    std::vector<RTSM*> msmtPts;

    I've overloaded the input operator '>>' for a value of _real (which is
    just a typedef for long double)

    //! Add a value to the time series
    INLINE RTSM& operator>>(_real& is, RTSM& rtsm){
    rtsm.bin[rtsm.bindex] = is;
    ++rtsm.bindex;
    return rtsm;
    }

    And I'm trying call the member function, and feed the value it returns
    into it's corresponding RTSM object. The code is this:

    void System::do_measurements(){
    for (_index i = 0; i<msmtPts.size(); ++i){
    _real sh_t = (bead.*msmtFPs)();
    (a) sh_t >>*(msmtPts);
    (b) bead.*(msmtFPs)()>> *(msmtPts);
    }
    }
    (a) works., (b) gives me:

    system.cpp: In member function `void System::do_measurements()':
    system.cpp:161: no match for `_real (Bead::*&)() >> RTSM&' operator
    results.hpp:24: candidates are: RM& operator>>(_real&, RM&)
    results.hpp:81: RTSM& operator>>(_real&, RTSM&)

    which I guess a few of you would predict. Although I may understant why
    this happens, I don't know how to get around it, other than by declaring
    a temporary variable like (a). Can someone educate me?

    Thanks,

    Glen Stark.
    (www.glenstark.org)
     
    glen stark, Sep 29, 2003
    #1
    1. Advertising

  2. "glen stark" <> wrote...
    > I'm working with an array of member function pointers (they are all get
    > function of the class Bead). The typedef is:
    >
    > typedef _real (Bead::*_beadGfp)(void);
    >
    > I have a class System which contains both an array of function pointers:
    >
    > std::vector<_beadGfp> msmtFPs;


    That's a vector specialised for _beadGfp, not an array.

    > And an array of pointers to RTSM, a class I've implemented for

    measurements.
    >
    > std::vector<RTSM*> msmtPts;


    This is not an array, as well. It's a vector. The two do differ.

    > I've overloaded the input operator '>>' for a value of _real (which is
    > just a typedef for long double)
    >
    > //! Add a value to the time series
    > INLINE RTSM& operator>>(_real& is, RTSM& rtsm){
    > rtsm.bin[rtsm.bindex] = is;
    > ++rtsm.bindex;
    > return rtsm;
    > }
    >
    > And I'm trying call the member function, and feed the value it returns
    > into it's corresponding RTSM object. The code is this:
    >
    > void System::do_measurements(){
    > for (_index i = 0; i<msmtPts.size(); ++i){
    > _real sh_t = (bead.*msmtFPs)();


    How is 'bead' declared? Are you sure the sizes are the same (so
    there is no logical error)? You're treading a thin line of too
    many assumptions here... It would be best to leave very little
    to imagination. Not that it matters too much, though...

    > (a) sh_t >>*(msmtPts);
    > (b) bead.*(msmtFPs)()>> *(msmtPts);
    > }
    > }
    > (a) works., (b) gives me:
    >
    > system.cpp: In member function `void System::do_measurements()':
    > system.cpp:161: no match for `_real (Bead::*&)() >> RTSM&' operator
    > results.hpp:24: candidates are: RM& operator>>(_real&, RM&)
    > results.hpp:81: RTSM& operator>>(_real&, RTSM&)
    >
    > which I guess a few of you would predict. Although I may understant why
    > this happens, I don't know how to get around it, other than by declaring
    > a temporary variable like (a). Can someone educate me?


    Make your operator>> accept a const _real& or just _real:

    RTSM& operator >> (_real, RTSM&);

    A temporary cannot be bound to a non-const reference.

    Victor
     
    Victor Bazarov, Sep 29, 2003
    #2
    1. Advertising

  3. Re: Passing a function pointer as an argument to an overloaded >>operator.

    glen stark wrote:
    > Hi all.
    >
    > I'm working with an array of member function pointers (they are all get
    > function of the class Bead). The typedef is:
    >
    > typedef _real (Bead::*_beadGfp)(void);


    _real and _beadGfp are reserved identifiers in the global namespace. I
    hope you are doing this inside your own namespace or a class.

    In general, identifiers beginning with an underscore may be used by the
    implementation, therefore you should probably not use them. Some
    identifiers of this form are allowed in some contexts, but in order to
    avoid problems it's probably best to avoid ever using an identifier that
    begins with an underscore.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 29, 2003
    #3
  4. glen stark

    glen stark Guest

    Re: Passing a function pointer as an argument to an overloaded >>operator.

    Victor Bazarov wrote:

    > "glen stark" <> wrote...
    >
    >>I'm working with an array of member function pointers (they are all get
    >>function of the class Bead). The typedef is:
    >>
    >> typedef _real (Bead::*_beadGfp)(void);
    >>
    >>I have a class System which contains both an array of function pointers:
    >>
    >>std::vector<_beadGfp> msmtFPs;

    >
    >
    > That's a vector specialised for _beadGfp, not an array.


    Yeah, ooops, I knew that... I just ran 30k on Sunday, that's my excuse,
    and I'm sticking to it.
    >>And an array of pointers to RTSM, a class I've implemented for

    >
    > measurements.
    >
    >>std::vector<RTSM*> msmtPts;

    >
    >
    > This is not an array, as well. It's a vector. The two do differ.

    See above

    >>I've overloaded the input operator '>>' for a value of _real (which is
    >>just a typedef for long double)
    >>
    >>//! Add a value to the time series
    >>INLINE RTSM& operator>>(_real& is, RTSM& rtsm){
    >> rtsm.bin[rtsm.bindex] = is;
    >> ++rtsm.bindex;
    >> return rtsm;
    >>}
    >>
    >>And I'm trying call the member function, and feed the value it returns
    >>into it's corresponding RTSM object. The code is this:
    >>
    >>void System::do_measurements(){
    >> for (_index i = 0; i<msmtPts.size(); ++i){
    >> _real sh_t = (bead.*msmtFPs)();

    >
    >
    > How is 'bead' declared? Are you sure the sizes are the same (so
    > there is no logical error)? You're treading a thin line of too
    > many assumptions here... It would be best to leave very little
    > to imagination. Not that it matters too much, though...


    bead is an instance of the Bead class (there's only one bead, so I was
    boring about the naming). Is that what you're asking? I'm not sure
    what you're getting at here.
    >
    >>(a) sh_t >>*(msmtPts);
    >>(b) bead.*(msmtFPs)()>> *(msmtPts);
    >> }
    >>}
    >>(a) works., (b) gives me:
    >>
    >>system.cpp: In member function `void System::do_measurements()':
    >>system.cpp:161: no match for `_real (Bead::*&)() >> RTSM&' operator
    >>results.hpp:24: candidates are: RM& operator>>(_real&, RM&)
    >>results.hpp:81: RTSM& operator>>(_real&, RTSM&)
    >>
    >>which I guess a few of you would predict. Although I may understant why
    >>this happens, I don't know how to get around it, other than by declaring
    >>a temporary variable like (a). Can someone educate me?

    >
    >
    > Make your operator>> accept a const _real& or just _real:
    >
    > RTSM& operator >> (_real, RTSM&);
    >
    > A temporary cannot be bound to a non-const reference.
    >
    > Victor


    Thanks, that's clear to me now.
     
    glen stark, Sep 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles Sullivan

    Passing function pointer as argument to a function???

    Charles Sullivan, Sep 19, 2005, in forum: C Programming
    Replies:
    17
    Views:
    1,229
    Emmanuel Delahaye
    Sep 21, 2005
  2. Vijai Kalyan
    Replies:
    4
    Views:
    708
    Vijai Kalyan
    Nov 8, 2005
  3. Angel Tsankov
    Replies:
    6
    Views:
    382
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    May 10, 2007
  4. , India
    Replies:
    8
    Views:
    444
    James Kanze
    Jul 19, 2010
  5. User1014
    Replies:
    1
    Views:
    186
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page