- Joined
- Aug 30, 2007
- Messages
- 2
- Reaction score
- 0
Hi.
I'd be very grateful if someone could shed some light on why the following code does not compile (compilation error below):
#include <iostream>
#include <vector>
using namespace::std;
template <class X>
class Base {
public:
virtual X get() = 0;
};
class Derived : public Base<int>{
private:
int x_;
public:
Derived(int x) : x_(x) {}
virtual int get(){
return x_;
}
};
template <class X>
class User {
public:
void foo(const vector<Base<X>*>& v){
for (int i=0; i<v.size(); ++i){
cout <<v->get() <<endl;
}
}
};
int main(void){
Derived d1(17);
Derived d2(33);
vector<Derived*> v;
v.push_back(&d1);
v.push_back(&d2);
User<int> u;
u.foo(v);
return 0;
}
The compilation error (using g++, cygwin) I get is:
a.cpp: In function `int main()':
a.cpp:42: error: no matching function for call to `User<int>::foo(std::vector<Derived*, std::allocator<Derived*> >&)'
a.cpp:25: note: candidates are: void User<X>::foo(const std::vector<Base<X>*, std::allocator<Base<X>*> >&) [with X = int]
Note that 'Derived' derives from 'Base<int>'.
Any help will be appreciated.
Thanks, Kfir.
I'd be very grateful if someone could shed some light on why the following code does not compile (compilation error below):
#include <iostream>
#include <vector>
using namespace::std;
template <class X>
class Base {
public:
virtual X get() = 0;
};
class Derived : public Base<int>{
private:
int x_;
public:
Derived(int x) : x_(x) {}
virtual int get(){
return x_;
}
};
template <class X>
class User {
public:
void foo(const vector<Base<X>*>& v){
for (int i=0; i<v.size(); ++i){
cout <<v->get() <<endl;
}
}
};
int main(void){
Derived d1(17);
Derived d2(33);
vector<Derived*> v;
v.push_back(&d1);
v.push_back(&d2);
User<int> u;
u.foo(v);
return 0;
}
The compilation error (using g++, cygwin) I get is:
a.cpp: In function `int main()':
a.cpp:42: error: no matching function for call to `User<int>::foo(std::vector<Derived*, std::allocator<Derived*> >&)'
a.cpp:25: note: candidates are: void User<X>::foo(const std::vector<Base<X>*, std::allocator<Base<X>*> >&) [with X = int]
Note that 'Derived' derives from 'Base<int>'.
Any help will be appreciated.
Thanks, Kfir.