implementing templated parentclass method

I'm trying to implement a method from one of my super classes but I seem to make some kind of mistake

the superclass:

A.h
1
2
3
4
5
6
7
template <class K, class V>
class A
{
public:
virtual K getKey();
virtual V getValue();
};


B.h
1
2
3
4
5
6
7
8
9
10
11
#include "A.h"
class B;

class B: public A<int, B>
{
private:
	int id;
public:
B(int id);
virtual ~B();
};


B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
B::B(int id)
{
	this->id = id;
}

B::~B()
{
}
int B::getKey()
{
	return this->id;
}


The method getKey gives the following error:

Description Resource Path Location Type
no ‘int B::getKey()’ member function declared in class ‘B’
Last edited on
Hello,
You should define the method in B to implement it.
You're missing the:
int getKey();
or
virtual int getKey();
in your B.h

It should work with that. Tell me.
I think because you are using templates that you should define both classes in the same header file so that compiler can find the template. Also for some reason I cannot understand the error message (a cannot determine its character encoding)
@sbonnalc
Do i have to declare the method again? Isn't it inherited from A? If I do declare the methods the errors go away, thank you, but it feels a bit silly to have to 're'-declare them.

@eypros
I forgot to copy the include(edited the original post), thank you. The error is what eclipse CDT tells me, there are some 'unreadable' characters.
@Floeps
Yes, you have to re-declare them if you want to re-implement them on your inherited class B.
If you use the inherited method without any change in the implementation, then, you don't have to re-declare them (like the getValue in your example).
I see, this is somewhat different from what I expected, as I had found that people use classes with virtual methods+destructors as (Java like) interfaces, this makes for a bit more work, but it should be okay, thank you.
Topic archived. No new replies allowed.