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

  • SEARCH
  • Home
  • 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 8573703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:18:42+00:00 2026-06-11T19:18:42+00:00

Possible Duplicate: Why can templates only be implemented in the header file? I ran

  • 0

Possible Duplicate:
Why can templates only be implemented in the header file?

I ran into this wall before, but I’ve no idea how to fix it. In g++ I get this error whenever I try to create an object of class BinaryTree:

/home/bej0843/cs261/Assignment1/main.cpp:9: undefined reference to `BinaryTree<char>::BinaryTree()'

Here’s the code for the header file:

#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;



template<typename Type>
class BinaryTree
{
    public:
        struct TreeNode
        {
                Type nodeinfo;
                BinaryTree<Type> *left;
                BinaryTree<Type> *right;
        };
        BinaryTree();
        void setInfo(Type a);
        void setSubtree(Type a);
        bool isEmpty();
        Type Info();
        void inOrder();
        void preOrder();
        void postOrder();
        virtual ~BinaryTree();
    protected:
        TreeNode *root;
        stack<TreeNode*> s;
        stack<TreeNode*> temp;
    private:
      void postOrder(TreeNode *r);
};


#endif  /* BINARYTREE_H */

And here’s the code for its implementation:

#include "BinaryTree.h"

template <typename Type>
BinaryTree<Type>::BinaryTree(){

    root = NULL;
}

template <typename Type>
void BinaryTree<Type>::setInfo(Type a){
  root->nodeinfo = a;
  root->left = NULL;
  root->right = NULL;
  s.push(root);
}

template <typename Type>
void BinaryTree<Type>::setSubtree(Type a){
  root->nodeinfo = a;
  root->left->root = s.top();
  s.pop();
  root->right->root = s.top();
  s.pop();
  s.push(root);
}

template <typename Type>
bool BinaryTree<Type>::isEmpty(){
  return (root==NULL);
}

template <typename Type>
Type BinaryTree<Type>::Info(){
  return root->nodeinfo;
}

template <typename Type>
void BinaryTree<Type>::inOrder(){

  TreeNode *c;
  c = s.top();

  while (c!=NULL || (!temp.empty())){
    if (c!=NULL)
    {
    temp.push(c);
    c = c->left;
    }
    else{
      c = temp.top();
      temp.pop();
      cout << c->nodeinfo +" ";
      c = c->right;
    }
  }

}

template <typename Type>
void BinaryTree<Type>::postOrder(){
  postOrder(s.top());
}

template <typename Type>
void BinaryTree<Type>::postOrder(TreeNode *r){
  temp.push(s.top());
  TreeNode *c = temp.top();
  s.pop();
  postOrder(c->left->root);
  postOrder(c->right->root);
  cout << c->nodeinfo + " ";

}

template <typename Type>
void BinaryTree<Type>::preOrder(){
  TreeNode*c = s.top();
  while (c!=NULL||(!temp.empty())){
    if (c!=NULL){
      cout << c->nodeinfo + " ";
      temp.push(c);
      c=c->left;
    }
    else{
      c=temp.top();
      temp.pop();
      c=c->right;
    }
  }
}

template <typename Type>
BinaryTree<Type>::~BinaryTree(){

}

In main I call:

BinaryTree<char> tree;

and get the error. 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-06-11T19:18:43+00:00Added an answer on June 11, 2026 at 7:18 pm

    You have to place the implementation and declaration of your class in the same file while working with template class.

    The compiler needs to generate the code at the same place where template class is used.

    Dynamic Allocation in Template Class Constructor

    You can do it like this.

    template <typename T>
    class myClass
    {
       //public and private interface. 
    } ;
    
    //Here the implementation of the interface goes, just beneath the declaration.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why can templates only be implemented in the header file? When I
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Why should
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Possible Duplicate: Why can templates only be implemented in the header file? “Undefined symbols”
Possible Duplicate: Why can templates only be implemented in the header file? I've been
Possible Duplicate: Why can templates only be implemented in the header file? I realise
Possible Duplicate: Why can templates only be implemented in the header file? I've struggled
Possible Duplicate: Why can templates only be implemented in the header file? hello everyone
Possible Duplicate: Why can templates only be implemented in the header file? Undefined symbol

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.