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