Any clue?

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.
 
Joined
Aug 30, 2007
Messages
2
Reaction score
0
The crux

Hi.

Apparently the problem is that there is no inheritance relationship between 'vector<Base<int>*>' and 'vector<Derived*>' although 'Dervied' derives from 'Base<int>'
 

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

Forum statistics

Threads
474,073
Messages
2,570,539
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top