Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:12+00:00 2026-05-27T09:11:12+00:00

My header file for a basic binary tree includes the following: template<typename T> class

  • 0

My header file for a basic binary tree includes the following:

template<typename T>
    class Btree{
        // OVERVIEW:    a binary tree with flexible structure that is not sorted

    private:

        struct node {           //a container object 
            node *left;     //left and right tree
            node *right;
            T *o;       // pointer to  object of node
        };

    public:

        node *root; //pointer to the root of the tree (NULL if empty)

        node* insert (node *parent, T *child, int child);
        //MODIFIES: this
        //EFFECTS:  creates a node that stores a pointer to the new child 
        //      and returns the pointer to the node of the new child    
        //      the integer child is either 0, for left child,
        //      or anything else for right child    

            //  void printTree (node * root);
        //EFFECTS:  takes the root of a tree and prints the tree's 
        //      coordinates  

        Btree(){};  //ctor
        Btree(){}   //dtor


    };

    #include "btree.cpp"

My .cpp looks like this, and note that it is included at the bottom of my header to avoid template compiler errors:

   template <typename T>
    Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child)

    {
        node *np = new node;
        np-> o = child; 
        np->left = NULL;
        np->right  = NULL;
        if (child == 0)
            parent->left = np;
        else
            parent->right = np;
        return np;
    } 

however, I get the following compiler error:

btree.cpp:3: error: expected constructor, destructor, or type conversion before ‘*’ token

I’m compiling with g++, version 4.1.2.

Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:11:13+00:00Added an answer on May 27, 2026 at 9:11 am

    First of all, your destructor should have a ~ in front of it, so change

    Btree();    //ctor
    Btree(){}   //dtor
    

    to

    Btree();    //ctor
    ~Btree(){}   //dtor
    

    Second, you need typename before the return type of insert because it is a dependent type:

       template <typename T>
       typename Btree<T>::node* Btree<T>::insert(node *parent, T *child, int child)
    // ^^^^^^^^ <- needed
       {
        node *np = new node;
        np-> o = child; 
        np->left = NULL;
        np->right  = NULL;
        if (child == 0)
            parent->left = np;
        else
            parent->right = np;
        return np;
       } 
    

    Also you need to rename one of your arguments, you have two named child.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my header file: class Factovisors { public: Factovisors(std::istream&
I have a class file (header and cpp) that I made, that I want
I have a header file with some inline template methods. I added a class
I have a c++ header file containing a class. I want to use this
Suppose a header file defines a function template. Now suppose two implementation files #include
For example: Base class header file has: enum FOO { FOO_A, FOO_B, FOO_C, FOO_USERSTART
I have a header file, lets say Common.h, that is included in all of
I have C header file containing the following type definition: // example.h typedef struct
Someone once hinted that doing this in a header file is not advised: using
I just created a new Xcode project. In the AppControl class Header file I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.