473,513 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL, std::transform and member functions

I don't know why this works:

class C {
public:
C(int x, int y) : a(x), b(y) {}
int a,b;
};

float avg(const C* c)
{
return ( (c->a) + (c->b) )/2.0;
}

class V {
void foo()
{
vector<C*> v;
v.push_back(new C(3,4));
v.push_back(new C(1,2));
v.push_back(new C(3,3));
transform(v.begin(),v.end(),ostream_iterator<float >(cout," "), avg);
}
};

And this doesn't:

class C {
public:
C(int x, int y) : a(x), b(y) {}
int a,b;
};

class V {
float avg(const C* c)
{
return ( (c->a) + (c->b) )/2.0;
}
void foo()
{
vector<C*> v;
v.push_back(new C(3,4));
v.push_back(new C(1,2));
v.push_back(new C(3,3));
transform(v.begin(),v.end(),ostream_iterator<float >(cout," "), avg);
// Compiler error here
}
};

How can I fix the 2nd?

Feb 26 '06 #1
2 6547
Alex wrote:
I don't know why this works: [example of using 'std::transform()' with a namespace level function]
And this doesn't: class C {
public:
C(int x, int y) : a(x), b(y) {}
int a,b;
};

class V {
float avg(const C* c)
{
return ( (c->a) + (c->b) )/2.0;
}
void foo()
{
vector<C*> v;
v.push_back(new C(3,4));
v.push_back(new C(1,2));
v.push_back(new C(3,3));
transform(v.begin(),v.end(),ostream_iterator<float >(cout," "), avg);
// Compiler error here
}
};


Well, first of all, your "avg" member function cannot be found
because it is nested into a class. You need to refer to this
function as 'V::avg'. However, this still does not work as this
is a method which requires an additional parameter: the object of
type 'V' you are operating on. Although the 'avg()' method does
not actually access any members (actually, there are none), the
object is still required. Thus, you need to create a functor and
bind an object:

V vobj;
std::transform(..., std::bind1st(std::mem_fun(&V::avg), &vobj));

Instead, you might want to create a functor yourself, e.g.
something like this:

struct avg {
float operator()(C* obj) const { return ... }
};

which is then passed as

std::transform(..., avg());

to the 'std::transform()' function.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 26 '06 #2
In article <11**********************@u72g2000cwu.googlegroups .com>,
"Alex" <ac******@gmail.com> wrote:
I don't know why this works:

class C {
public:
C(int x, int y) : a(x), b(y) {}
int a,b;
};

float avg(const C* c)
{
return ( (c->a) + (c->b) )/2.0;
}

class V {
void foo()
{
vector<C*> v;
v.push_back(new C(3,4));
v.push_back(new C(1,2));
v.push_back(new C(3,3));
transform(v.begin(),v.end(),ostream_iterator<float >(cout," "), avg);
}
};

And this doesn't:

class C {
public:
C(int x, int y) : a(x), b(y) {}
int a,b;
};

class V {
float avg(const C* c)
{
return ( (c->a) + (c->b) )/2.0;
}
void foo()
{
vector<C*> v;
v.push_back(new C(3,4));
v.push_back(new C(1,2));
v.push_back(new C(3,3));
transform(v.begin(),v.end(),ostream_iterator<float >(cout," "), avg);
// Compiler error here
}
};

How can I fix the 2nd?


try making the 'avg' member-function in the second case static...

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3648
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,...
8
9034
by: Dilip | last post by:
I was reading the C++ templates (Josuttis et al) book and came across an example that left me scratching my head.. there is a function template: template<typename T, int val> T addvalue(T...
4
3332
by: Ney André de Mello Zunino | last post by:
Hello. The following program: #include <list> #include <iterator> #include <algorithm> #include <cmath> #include <iostream>
8
2101
by: rupert | last post by:
It seems a need a paradime shift in understanding how C++ use libraries. Sun keeps is together under their API. And Javadoc enables a neat way of displaying functions of a local program to display...
6
4940
by: Vijai Kalyan | last post by:
Hello, I am trying to use std::transform to take in a collection of strings, transform them and insert the result into an output container. So, I wrote something like this: std::wstring...
2
2709
by: kvnil | last post by:
Cheers, I've been struggling all day today with this, maybe you could give me some useful pointers. I would like to (elegantly) concatenate a string out of all node values in a map. I have to...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.