Home > Archive > VC Language > June 2005 > Template class problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Template class problem
|
|
| Dr. Proctor 2005-06-10, 8:59 am |
| Hello
I have written an STL functor that calculates sum of the elements in a
container. It is to be used with for_each algorithm;
----------------
template<class T>
class stl_sum
{
public:
stl_sum(): s(0) { }
void operator()(T& val)
{
s+= val;
}
T Get();
private:
T s;
};
----------------
My problem is that assigning the value from Get() causes compiler
error C2228: left of '.sum' must have class/struct/union type.
Why is this happening? Here's the code:
----------------
stl_sum<float> sum();
float f;
std::for_each(samples.begin(), samples.end(), sum);
f = sum.Get()/samples.size();
----------------
TIA.
| |
| Dr. Proctor 2005-06-10, 8:59 am |
| On Fri, 10 Jun 2005 10:36:22 GMT, Dr. Proctor <user@usa.net> wrote:
>Hello
>
>I have written an STL functor that calculates sum of the elements in a
>container. It is to be used with for_each algorithm;
>
>----------------
>template<class T>
>class stl_sum
>{
>public:
> stl_sum(): s(0) { }
>
> void operator()(T& val)
> {
> s+= val;
> }
>
> T Get();
>
>private:
> T s;
>};
>----------------
>
>My problem is that assigning the value from Get() causes compiler
>error C2228: left of '.sum' must have class/struct/union type.
>Why is this happening? Here's the code:
>
>----------------
>stl_sum<float> sum();
>float f;
>
>std::for_each(samples.begin(), samples.end(), sum);
>
>f = sum.Get()/samples.size();
>----------------
>
>TIA.
Sorry for confusion, the error message was: left of '.Get' not '.sum'.
| |
| Tom Widmer 2005-06-10, 8:59 am |
| Dr. Proctor wrote:
> Hello
>
> I have written an STL functor that calculates sum of the elements in a
> container. It is to be used with for_each algorithm;
>
> ----------------
> template<class T>
> class stl_sum
> {
> public:
> stl_sum(): s(0) { }
>
> void operator()(T& val)
void operator()(T const& val)
> {
> s+= val;
> }
>
> T Get();
T Get() const
{
return s;
}
>
> private:
> T s;
> };
> ----------------
>
> My problem is that assigning the value from Get() causes compiler
> error C2228: left of '.sum' must have class/struct/union type.
> Why is this happening? Here's the code:
>
> ----------------
> stl_sum<float> sum();
That declares a function called "sum" that takes no parameters and
returns an stl_sum<float>! I think you meant:
stl_sum<float> sum;
> float f;
>
> std::for_each(samples.begin(), samples.end(), sum);
>
> f = sum.Get()/samples.size();
Better would be:
#include <numeric>
//....
float f = std::accumulate(samples.begin(), samples.end(), 0.0f);
or, better:
double f = std::accumulate(samples.begin(), samples.end(), 0.0);
Note that double is often faster than float unless you are memory
bandwidth limited.
Tom
| |
| Simon Trew 2005-06-11, 3:59 am |
| "Tom Widmer" <tom_usenet@hotmail.com> wrote in message
news:OuYDyqabFHA.584@TK2MSFTNGP15.phx.gbl...
> Better would be:
> #include <numeric>
> //....
> float f = std::accumulate(samples.begin(), samples.end(), 0.0f);
>
> or, better:
> double f = std::accumulate(samples.begin(), samples.end(), 0.0);
> Note that double is often faster than float unless you are memory
> bandwidth limited.
>
> Tom
Sorry Tom, am I missing something here? Since std::accumulate works on the
value_type of the collection (which I guess here is float though I can't see
it defined in the original posting), any improvement in speed for the
assignment to the result type would surely be trivial compared to that for
the accumulate function itself. Perhaps it's reasonable to recommend making
samples' value_type be double, but since we don't actually know what it is
right now (only that it is assignable to float, constructable(?) from float
and has an operator+) I wonder why you say this.
Certainly it's appropriate in some applications to use floats for storage
and doubles for computation. (And similarly with narrower or wider types to
express integers.)
Of course you are right about the function declaration.
S.
| |
| Arnaud Debaene 2005-06-11, 8:58 am |
| Dr. Proctor wrote:
> Hello
>
> I have written an STL functor that calculates sum of the elements in a
> container. It is to be used with for_each algorithm;
Even with the correction given by Tom, it won't work the way you coded it
because functors are passed by value to algorithms (that is, the stl_sum
object used by for_each is a copy of your local "sum" variable).
Your stl_sum functor hasn't got correct copy semantic : you should add copy
constructor and operator=, and make sure that all copies share the same
underlying counter (a boost::shared_ptr seems to be in order here)
Arnaud
MVP - VC
|
|
|
|
|