NewbCOder10
NewbCOder10

Reputation: 29

Base Class Constructor not being accessed in Derived Class

I am trying to learn C++ policy based design and wrote the following snippet, and I got the following error:

 class 'Binary_Tree<DataType, TreeNode, Traversal_Policy>' does not have any field named 'Tree_Base'
   90 |         Binary_Tree(DataType val,Traversal_Policy traversal_policy_type):Root(val),Tree_Base(traversal_policy_type){};

Not sure where I am making a mistake as Tree Base should be directly accessible from Binary_Tree

class Tree_Traversal_Policy
{
    public:
        enum traversal_type{PREFIX=0,INFIX,POSTFIX};
        Tree_Traversal_Policy(traversal_type type):m_traversal_type(type){};
    private:
        traversal_type m_traversal_type;
};
template <typename DataType,template <typename> typename TreeNode,typename Traversal_Policy=Tree_Traversal_Policy>
class Tree_Base:public Traversal_Policy
{
    public:
        Tree_Base(Traversal_Policy traversal_policy_type):Traversal_Policy(traversal_policy_type){};
};
template <typename DataType,template < typename > typename TreeNode,typename Traversal_Policy>
class Binary_Tree:public Tree_Base<DataType,TreeNode,Traversal_Policy>
{
    using BTreeNode_ = TreeNode <DataType>;
    BTreeNode_ Root;
    public:
        Binary_Tree(DataType val,Traversal_Policy traversal_policy_type):Root(val),Tree_Base(traversal_policy_type){};
        void add_node(DataType val);
        void delete_node(DataType val);
};

Upvotes: 1

Views: 157

Answers (1)

Sneftel
Sneftel

Reputation: 41542

The ability to omit the template arguments from the use of an instantiated template class is limited to the body of the templated class itself. You can refer to Tree_Base simply as Tree_Base only in Tree_Base; in Binary_Tree and elsewhere you must qualify it with the template arguments. So:

Binary_Tree(DataType val,Traversal_Policy traversal_policy_type):
    Root(val),
    Tree_Base<DataType,TreeNode,Traversal_Policy>(traversal_policy_type) 
{};

Note that it's common to do something like using Base = Tree_Base<DataType,TreeNode,Traversal_Policy> in the class definition to avoid all that extra typing. If you do that, you can simply refer to it as Base in the initializer.

Upvotes: 3

Related Questions