STL, std::transform and member functions

A

Alex

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?
 
D

Dietmar Kuehl

Alex said:
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.
 
D

Daniel T.

"Alex said:
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...
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top