trouble with derivate class using template

I get this error:

 In member function 'data hand<data>::getfingers()':
18:29: error: 'fingers' was not declared in this scope
 In instantiation of 'data hand<data>::getfingers() [with data = int]':
23:23:   required from here
18:46: warning: no return statement in function returning non-void [-Wreturn-type]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

template < typename data > class human {
protected:
	data fingers;
public:
	human(data a) : fingers(a) {}
	virtual ~human() {}
    data getfingers() { return fingers; }
};

template <typename data> class hand : public human< data > {
public:

	hand(int a=50) : human<data>(a) {}
	virtual ~hand(){}
	data getfingers() { return fingers+fingers; }
};

int main (){
	hand<int> q(4);
	cout << q.getfingers() << endl;
	return 0;
}



If I don't redefine 'getfingers()' at class 'hand' it works just fine.
Last edited on
1
2
3
4
5
6
7
8
9
10
template <typename data> class hand : public human< data > {
public:

	hand(int a=50) : human<data>(a) {}
	virtual ~hand(){}

	// data getfingers() { return fingers+fingers; }
        // clarify that fingers is a dependent name
        data getfingers() { return this->fingers + this->fingers ; }
};


See: 'Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?' in https://isocpp.org/wiki/faq/templates
Thank you very much!
Topic archived. No new replies allowed.