• Advertisement
Sign in to follow this  

Inherited template typedefs

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// helper.h
namespace Helper {
	template <class T>
	struct allocHelper {
		typedef T&       reference;
	};
}
//
#include "helper.h"
template <class T>
class Tree : public Helper::allocHelper<T> {
public:
	typedef typename Helper::allocHelper<T>::reference reference;

	void fn(reference r) {
		r = r;
	}
}

template <class T>
class SelfBalancedTree : public Tree<T> {
public:
	void fn2(reference rr) {
		rr = rr;
	}
}
fn() compiles fine, but I'm getting the following error in fn2(): error: `reference' has not been declared error: ISO C++ forbids declaration of `rr' with no type

Share this post


Link to post
Share on other sites
Advertisement
I'm no template whizz, but try this:

template <class T>
class SelfBalancedTree : public Tree<T> {
public:
void fn2(typename Tree<T>::reference rr) {
rr = rr;
}
}

Share this post


Link to post
Share on other sites
It's because reference does not exist in SelfBalancedTree<T>, it exits in Tree<T>, so you have to qualify its namespace fully.
It might be sensible to re-typedef it in the subclass.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement