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

G

glen stark

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)
 
V

Victor Bazarov

glen stark said:
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
 
K

Kevin Goodsell

glen said:
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
 
G

glen stark

Victor said:
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.
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top